在我的应用程序中包含像facebook button, twitter button, logout button, imageview with gestures
这样的UIElements
'
facebookBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[facebookBtn setImage:@"facebook.png"];
[facebookBtn addTarget:self action:@selector(switchToFacebookShare) forControlEvents:UIControlEventTouchUpInside];
[facebookBtn setFrame:CGRectMake(0, 50, 24, 24)];
[self.view addsubview:facebookBtn];`
`twitterBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[twitterBtn setImage:@"facebook.png"];
[twitterBtn addTarget:self action:@selector(switchToTwitterShare) forControlEvents:UIControlEventTouchUpInside];
[twitterBtn setFrame:CGRectMake(0, 100, 24, 24)];
[self.view addsubview:twitterBtn];`
`imageTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionTapping:)];
[imageTapGesture setNumberOfTapsRequired:1];`
`sectionPortraitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[sectionPortraitImageView setUserInteractionEnabled:YES];
[sectionPortraitImageView addGestureRecognizer:imageTapGesture];
[contentView addSubview:sectionLandscapeImageView];`
像这样,我添加了所有元素当用户一次单击所有按钮时,所有三个功能都正常工作。这里只需要执行一个操作。
如何解决这个问题?
一次处理多个触摸问题并设置单个属性
[sectionLandscapeImageView setExclusiveTouch:YES];
处理所有子视图多触摸用户交互
-(void)multipleTouchHandling
{
self.view.multipleTouchEnabled=NO;
self.view.exclusiveTouch=YES;
for(UIView* view1 in self.view.subviews)
{
if([view1 isKindOfClass:[UIButton class]])
{
UIButton* btnVw = (UIButton*)view1;
[btnVw setExclusiveTouch:YES];
}
}
}
我看到了摆脱这种情况的几种方法。
-
您保留指向每个按钮的强链接,并使
[someButton setUserInteractionEnabled:NO]
对于用户单击某人后的每个元素。但这需要大量的时间和代码
- (我认为更好的解决方案)或者,您可以在视图顶部显示
UIActivityIndicatorView
,以在处理某些数据或执行其他操作时阻止所有用户交互。