为什么我的视图无法识别UIPanGestureRecognizer?

时间:2016-08-31 09:28:13

标签: ios uigesturerecognizer uipangesturerecognizer

当我标记图像时,没有显示的内容?为什么呢?

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:0)

userInteractionEnabled类中NO属性的默认值设置为UIImageView。也许那是你的问题。

From UIImageView Reference:

  

此属性继承自UIView父类。此类将此属性的默认值更改为NO。

检查此片段代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:imageView];

    imageView.userInteractionEnabled = YES;

    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureRecognized:)];
    [imageView addGestureRecognizer:panGesture];
}

- (void)panGestureRecognized:(UIGestureRecognizer *)gestureRecognizer {
    NSLog(@"It Works!");
}

答案 1 :(得分:0)

应用此代码,工作正常:

-(void)initialization
 UIPanGestureRecognizer *panRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)] autorelease];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [imageview addGestureRecognizer:panRecognizer];
    imageview.userInteractionEnabled = YES;
 }
- (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]];
    }
}

答案 2 :(得分:0)

- (void)viewDidLoad {
    [super viewDidLoad];

    _view2 = [[V0 alloc ]initWithFrame:self.view.bounds];
    CGRect a = self.view2.frame;
    a.origin.x = 100;
    a.origin.y = 150;
    _view2.frame = a;
    _view2.userInteractionEnabled = YES;

    [self.view2 addGestureRecognizer:[[UIPanGestureRecognizer alloc]initWithTarget:self.view2 action:@selector(Up:)]];

我添加了initWithFrame,然后我的视图可以识别我的手势