经过近4天的搜索找到问题的解决方案,我现在问你,希望能帮忙。
我的UIViewController
只有一个UIViewContainer
。将使用从nib文件加载的View以编程方式填充此容器。最后一个视图附有3个手势识别器。但是没有一个被击中。
使用ViewContainer的主视图
笔尖视图
将view.nib加载到容器中的代码
userMasterViewContainer *container = [[[[NSBundle mainBundle] loadNibNamed:@"UserCardView" owner:nil options:nil] lastObject] initWithDelegate:self andUser:user];
[self.viewContainer addSubview:container];
nib视图类中的手势识别器代码
- (void)addGestureRecognizerToPicture:(UIView *)picture
{
// profile picture gestures
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotatePicture:)];
[picture addGestureRecognizer:rotationGesture];
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scalePicture:)];
[pinchGesture setDelegate:self];
[picture addGestureRecognizer:pinchGesture];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panPicture:)];
[panGesture setMaximumNumberOfTouches:2];
[panGesture setDelegate:self];
[picture addGestureRecognizer:panGesture];
self.userInteractionEnabled = YES;
}
- (void)rotatePicture:(id)rotateProfilePicture
{
}
- (void)scalePicture:(id)scaleProfilePicture
{
}
- (void)panPicture:(UIPanGestureRecognizer *)gestureRecognizer
{
}
手势将在viewDidLoad中添加到两个UIImageView
:封面叠加和用户照片
[self addGestureRecognizerToPicture:self.backgroundPicture];
他们都没有工作。我希望有人可以帮助我。
最好的问候安德烈
答案 0 :(得分:2)
经过几天的努力,我解决了问题。 有一个奇怪的家长观点组合捕捉我的用户互动。
我通过为每个不应识别触摸的视图将userInteractionEnabled
属性设置为NO
来解决此问题。
// setup inactive and active elements for user interactions
self.userInteractionEnabled = YES;
self.userPicture.userInteractionEnabled = YES;
self.buttonOverlay.userInteractionEnabled = NO;
self.backgroundPictureOverlay.userInteractionEnabled = NO;
self.userPictureOverlay.userInteractionEnabled = NO;
因为我的视图中还有一个UIButton Button - Profile
。还需要以下代码来阻止他捕捉手势。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
return touch.view == self.buttonOverlay ? NO : YES;
}
另外,我必须添加以下方法,因为我想在简单的图像编辑器中使用这些手势。使用此代码,我可以同时旋转,缩放和平移。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}