如何将UIPanGestureRecognizer添加到UIImageView

时间:2011-11-16 12:37:42

标签: iphone ipad

-(void)initialization
UIPanGestureRecognizer *panRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)] autorelease];
        [panRecognizer setMinimumNumberOfTouches:1];
        [panRecognizer setMaximumNumberOfTouches:1];
        [panRecognizer setDelegate:self];
        [imageview addGestureRecognizer:panRecognizer];
 }


    - (void)move:(UIPanGestureRecognizer *)gestureRecognizer
    {
        UIView *piece = imageview;

        [self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

        if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
            CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
            CGPoint translatedCenter = CGPointMake([piece center].x + translation.x, [piece center].y + translation.y);
            CGPoint center = [self centerWithBounds:translatedCenter andViewFrame:[piece frame] andBoundingFrame:[[piece superview] frame]];
            [piece setCenter:center];
            [gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
        }
    }

我将手势添加到imageview,而不是调用移动动作。

如何仅将手势添加到UIImageView ..

1 个答案:

答案 0 :(得分:19)

尝试设置imageview.userInteractionEnabled = YES。默认情况下,UIImageViews userInteractionEnabled设置为NO