UILongPressGestureRecognizer位于导航栏的视图中

时间:2013-11-30 17:03:14

标签: ios uigesturerecognizer

我正在使用此代码在导航栏上显示圆形用户的头像:

UIView *avatView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 190.0f)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(-20, 75, 40, 40)];
imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
imageView.image = [UIImage imageWithData:myUtente.imgData];
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 20.0;
imageView.layer.borderColor = [UIColor blueColor].CGColor;
imageView.layer.borderWidth = 1.0f;
imageView.layer.rasterizationScale = [UIScreen mainScreen].scale;
imageView.layer.shouldRasterize = YES;
imageView.clipsToBounds = YES;
[avatView addSubview:imageView];
self.navBar.titleView = avatView;

现在我希望这个化身像按钮一样按下2秒钟,所以我添加了这段代码:

UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(switchUser:)];
[longPressGesture setMinimumPressDuration:2.0f];
// same code as above
[avatView addGestureRecognizer:longPressGesture];

相应的动作永远不会被调用,我不知道为什么。任何的想法? 感谢。

2 个答案:

答案 0 :(得分:0)

这个链接有一个关于如何使用手势识别器的好教程,你可以尝试一下http://www.raywenderlich.com/6567/ 虽然它有平移,捏合,旋转手势的例子,但你可以用长按手势尝试这些程序。

答案 1 :(得分:0)

更改avatViewimageView的框架,然后手势就像魅力一样。

前:

UIView *avatView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 2, 40, 40)];