简体   繁体   中英

UILongPressGestureRecognizer on a navigationbar's view

I'm using this code to show a round user's avatar on my navigation bar:

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;

Now I'd like this avatar to act like a button, with a 2 seconds long press on it, so I've added this code:

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

The corresponding action never gets called and I don't know why. Any idea? Thanks.

此链接提供了有关如何使用手势识别器的很好的教程,您可以尝试一下http://www.raywenderlich.com/6567/尽管它具有平移,捏合,旋转手势的示例,但是您可以长按尝试这些步骤手势。

Change the frame of avatView and imageView then gesture works like a charm.

ex:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM