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];  

}
3
スーパービューのフレームを制限するだけです
追加された 著者 kiran,
どのようにして、指定された範囲で動画を制限するか考えている人がいます。
追加された 著者 user905582,

2 答え

まず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];
                    }
                }

    }
1
追加された

次のコードは、スーパービュー境界内の動画像を制限します

if (recognizer.state == UIGestureRecognizerStateBegan)
{
    previousPoint = [recognizer translationInView:self];
}
else if (recognizer.state == UIGestureRecognizerStateChanged)
{

    CGRect parentFrame = self.frame;
    CGRect currentFrame = imageView.frame;

    if (CGRectContainsRect(parentFrame, currentFrame)) 
    {
        CGPoint point = [recognizer translationInView:self.superview];
        CGPoint movePoint = CGPointMake(point.x - previousPoint.x, point.y - previousPoint.y);
        CGPoint newCenter = CGPointMake(imageView.center.x + movePoint.x, imageView.center.y + movePoint.y);
        imageView.center = newCenter;
        previousPoint = point;
    }
    else
    {
        CGRect imageRect = imageView.frame;
        if (imageRect.origin.x < 0)
            imageRect.origin.x = 0;
        else if (imageRect.origin.x + imageRect.size.width >= self.frame.size.width)
            imageRect.origin.x = self.frame.size.width - imageRect.size.width;

        if (imageRect.origin.y < 0)
            imageRect.origin.y = 0;
        else if (imageRect.origin.y + imageRect.size.height >= self.frame.size.height)
            imageRect.origin.y = self.frame.size.height - imageRect.size.height;

        imageView.frame = imageRect;
    }
}
0
追加された