UIViewとUIImageViewの2つのビューを持つGestureRecognizerの問題

私はUIImageViewにジェスチャーを適用しようとしています。
画像ビューワーキングでのジェスチャーの回転と移動だから私は、画像を回転し、拡大縮小し、ある点から別の点に移動するのを見ることができます。

私が画像とズームレベルを最小にし、UIImageViewのスーパークラスであるUIViewに触れて、ジェスチャーアクションを与えたとき。
UIImageViewでのみ動作する必要のあるジェスチャをUIViewに制限する方法。
Imageviewがビューの外に出る必要はありません。

-(void)InitGestures{



    tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    tapRecognizer.numberOfTouchesRequired = 1;
    templatePhotoPlaceholderView=[[UIView alloc]init]; 
    templatePhotoPlaceholderView.clipsToBounds = YES;


    //templatephotoplaceholder frame setting
    [self templatePhotoPlaceholderFrameSetting];

    templatePhotoPlaceholderView.backgroundColor=[UIColor colorWithRed:0.8823 green:0.8823 blue:0.8823 alpha:1];
    [self photoView:templatePhotoPlaceholderView];

    tapRecognizer.view.frame=templatePhotoPlaceholderView.frame;
    [self photoButtonPlaceHolder:templatePhotoPlaceholderView];

    [selectedTemplateImage addSubview:templatePhotoPlaceholderView];
    [templatePhotoPlaceholderView addGestureRecognizer:tapRecognizer];



    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOfTouches:1];

    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [templatePhotoPlaceholderView addGestureRecognizer:panRecognizer];
    //[panRecognizer release];


    UIPinchGestureRecognizer *pinchRecognizer = [[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)] autorelease];
    [pinchRecognizer setDelegate:self];
    [self.view addGestureRecognizer:pinchRecognizer];
    //[pinchRecognizer release];

    [self.view addSubview:templatePhotoPlaceholderView];
    [tapRecognizer release];

}


-(void)move:(UIPanGestureRecognizer *)gestureRecognizer
{
    CGPoint translatedPoint = [gestureRecognizer translationInView:templatePhotoPlaceholderView];

    if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        _firstX = [imageview center].x;
        _firstY = [imageview center].y;
    }



    translatedPoint = CGPointMake(_firstX+translatedPoint.x, _firstY+translatedPoint.y);

    [imageview setCenter:translatedPoint];  

}
ベストアンサー

まずUIImageViewをUIViewに追加します。各UIViewにジェスチャーを適用します。
UIImageViewの画像を変更します。回転のように見えます。次のコードを攻撃しています。それを試してください。このサンプルは3
UIView用です。

  -(void)addSwipeEvent:(UIView*)subView{

        UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
        recognizer.numberOfTouchesRequired = 1;
        recognizer.delegate = self;
        [subView addGestureRecognizer:recognizer];
        [recognizer release];

        UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
        leftRecognizer.direction=UISwipeGestureRecognizerDirectionLeft;
        leftRecognizer.numberOfTouchesRequired = 1;
        recognizer.delegate = self;
        [subView addGestureRecognizer:leftRecognizer];
        [leftRecognizer release];

        UISwipeGestureRecognizer *downRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
        downRecognizer.direction=UISwipeGestureRecognizerDirectionDown;
        downRecognizer.numberOfTouchesRequired = 1;
        recognizer.delegate = self;
        [subView addGestureRecognizer:downRecognizer];
        [downRecognizer release];

        UISwipeGestureRecognizer *upRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(SwipeRecognizer:)];
        upRecognizer.direction=UISwipeGestureRecognizerDirectionUp;
        upRecognizer.numberOfTouchesRequired = 1;
        recognizer.delegate = self;
        [subView addGestureRecognizer:upRecognizer];
        [upRecognizer release];
    }

    - (void) SwipeRecognizer:(UISwipeGestureRecognizer *)sender {
        if ( sender.direction == UISwipeGestureRecognizerDirectionLeft ){
            NSLog(@" *** SWIPE LEFT ***");

        }
        if ( sender.direction == UISwipeGestureRecognizerDirectionRight ){
            NSLog(@" *** SWIPE RIGHT ***");

        }
        if ( sender.direction== UISwipeGestureRecognizerDirectionUp ){
            NSLog(@" *** SWIPE UP ***");

        }
        if ( sender.direction == UISwipeGestureRecognizerDirectionDown ){
            NSLog(@" *** SWIPE DOWN ***");

        }
    }

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
        if ([touch.view isKindOfClass:[UIView class]])
        {
            viewIndex = touch.view.tag;
            return YES;
        }
        return NO;
    }

    -(void)lowLevelSwipeRecognizer{

            CATransition *myTransition = [CATransition animation];
            myTransition.type = kCATransitionPush;
            myTransition.duration = 0.3;
            myTransition.subtype = kCATransitionFromRight;

                NSString *str = [arrayImg objectAtIndex:0];

                for (int i=0; i<2; i++) {
                    [arrayImg replaceObjectAtIndex:i withObject:[arrayImg objectAtIndex:i+1]];

                }
                [arrayImg replaceObjectAtIndex:2 withObject:str];


                for (int cnt = 0; cnt<3; cnt++) {
                    if ([[arrayView  objectAtIndex:cnt] isKindOfClass:[CustomView class]]) {
                        CustomView *v = (CustomView *)[arrayView  objectAtIndex:cnt];
                        [v.imageView setImage:[UIImage imageNamed:[arrayImg objectAtIndex:cnt]]];
                        [v.layer addAnimation:myTransition forKey:nil];
                    }
                }

    }

コメントする

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です