0

ジェスチャーを追跡し、アクションをコミットするようにビューコントローラーを設定しました。この場合は、タブを変更します。ビューコントローラーのコードは次のとおりです。

#define HORIZ_SWIPE_DRAG_MIN 100
CGPoint mystartTouchPosition;
BOOL isProcessingListMove;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint newTouchPosition = [touch locationInView:self.view];
    if(mystartTouchPosition.x != newTouchPosition.x || mystartTouchPosition.y != newTouchPosition.y) {
        isProcessingListMove = NO;
    }
    mystartTouchPosition = [touch locationInView:self.view];
    [super touchesBegan:touches withEvent:event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = touches.anyObject;
    CGPoint currentTouchPosition = [touch locationInView:self.view];

    // If the swipe tracks correctly.
    double diffx = mystartTouchPosition.x - currentTouchPosition.x + 0.1; // adding 0.1 to avoid division by zero
    double diffy = mystartTouchPosition.y - currentTouchPosition.y + 0.1; // adding 0.1 to avoid division by zero

    if(abs(diffx / diffy) > 2.5 && abs(diffx) > HORIZ_SWIPE_DRAG_MIN)
    {
        // It appears to be a swipe.
        if(isProcessingListMove) {
            // ignore move, we're currently processing the swipe
            return;
        }

        if (mystartTouchPosition.x < currentTouchPosition.x) {
            isProcessingListMove = YES;
            self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
            return;
        }
        else {
            isProcessingListMove = YES;

            self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:2];

            return;
        }
    }
    else if(abs(diffy / diffx) > 1)
    {
        isProcessingListMove = YES;
        [super touchesMoved:touches withEvent:event];
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    isProcessingListMove = NO;
    [super touchesEnded:touches withEvent:event];
}

ビューコントローラーのビューの下に設定した uiwebview で同じジェスチャーで同じことをしたいと思います。つまり、uiwebview がビュー上にあります。スワイプすると、基本的な uiwebview 関数 (スクロール) が表示されます。uiviewcontroller と同じコードでカスタム uiwebview クラスをセットアップすると、エラーが発生します。uiwebview に合うように、機能を損なわずにそのコード ブロックを変換する方法を知る必要があります。

4

2 に答える 2

1

UIWebView をサブクラス化し、それらのメソッドをオーバーライドできるはずです。ただし、すべての touch メソッドをオーバーライドする必要がある場合があります。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

編集:ああ、私は今あなたのエラーを見ます。それらは、親に行ったように、これらのことが UIWebView に適用されないためです(すでに知っています...)

UIWebView は UIView であるため、置き換えることができるはずです

[touch locationInView:self.view]

[touch locationInView:self]

ただし、親のタブ バー コントローラーにアクセスするには、別の方法を設計する必要があります。親への参照を UIWebView サブクラスに渡し、アクティブなタブを変更するための親でメソッドを公開できます。たとえば、次のようになります。

サブクラスで、親タブ バー コントローラーのプロパティを追加します。

UITabBarController *parent;

UIWebView サブクラスを作成してタブ バー コントローラーに追加するときは、親も設定します。

webView.parent = self;

これを Interface Builder で設定する場合は、親を IBOutlet にして、IB を介して接続します。

タブ バー コントローラー ビューで、選択したコントローラーを変更するメソッドを追加します。切り替え先のビューに名前付き定数を使用するか、切り替え先を正確に記述するメソッドを使用できますが、簡単にするために、概念的に次のようにします。

- (void) switchToView: (int)viewNumber 
{
    self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:viewNumber]; 
}

次に、コードを呼び出し[parent switchToView:2]ます。

別の間違いなくより良い方法は、NSNotificationを使用して、親をリスナーとして登録することです。

于 2010-02-16T19:44:08.077 に答える
0

私の完全なコードは次のとおりです。

#import "CustomWebView.h"

@implementation CustomWebView

//Swipe between tabs
#define HORIZ_SWIPE_DRAG_MIN 100
CGPoint mystartTouchPosition;
BOOL isProcessingListMove;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint newTouchPosition = [touch locationInView:self.view]; "error: request for member 'view' in something not a structure or union"


    if(mystartTouchPosition.x != newTouchPosition.x || mystartTouchPosition.y != newTouchPosition.y) {
        isProcessingListMove = NO;
    }
    mystartTouchPosition = [touch locationInView:self.view]; "error: request for member 'view' in something not a structure or union"
    [super touchesBegan:touches withEvent:event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch *touch = touches.anyObject;
    CGPoint currentTouchPosition = [touch locationInView:self.view]; "error: request for member 'view' in something not a structure or union"

    // If the swipe tracks correctly.
    double diffx = mystartTouchPosition.x - currentTouchPosition.x + 0.1; // adding 0.1 to avoid division by zero
    double diffy = mystartTouchPosition.y - currentTouchPosition.y + 0.1; // adding 0.1 to avoid division by zero

    if(abs(diffx / diffy) > 2.5 && abs(diffx) > HORIZ_SWIPE_DRAG_MIN)
    {
        // It appears to be a swipe.
        if(isProcessingListMove) {
            // ignore move, we're currently processing the swipe
            return;
        }

        if (mystartTouchPosition.x < currentTouchPosition.x) {
            isProcessingListMove = YES;
            self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0]; "error: request for member 'tabBarController' in something not a structure or union"
            return;
        }
        else {
            isProcessingListMove = YES;

            self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:2]; "error: request for member 'tabBarController' in something not a structure or union"

            return;
        }
    }
    else if(abs(diffy / diffx) > 1)
    {
        isProcessingListMove = YES;
        [super touchesMoved:touches withEvent:event];
    }
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{
    isProcessingListMove = NO;
    [super touchesEnded:touches withEvent:event];
}
// End of swipe

@end

エラーは次のとおりで、コードに含まれています。

助けてくれてありがとう。

于 2010-02-16T19:47:25.547 に答える