我正在努力熟悉IOS提供的每个手势,以便我可以将它们应用到我的项目中。我有水龙头和长按工作正常,但由于某种原因我的捏和旋转不会出现。我尝试了一些其他的事情,比如添加一个委托,并将它与其他代码进行比较,我看不出有什么不对。代码的第一部分是我的视图加载,其余的是手势的每种方法。
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
int sw = self.view.frame.size.width;
_tapped = [[UILabel alloc] initWithFrame:CGRectMake(sw/2-90, 30, 300, 30)];
[self.view addSubview:_tapped];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapReconginzed:)];
[tap setNumberOfTapsRequired:5];
[self.view addGestureRecognizer:tap];//adds the gesture to the view
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressReconginzed:)];
//[longPress numberOfTouchesRequired:1];
[longPress setNumberOfTapsRequired:1];
[longPress setMinimumPressDuration:3];
[self.view addGestureRecognizer:longPress];
UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateReconginzed:)];
[self.view addGestureRecognizer:rotate];
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchReconginzed:)];
[self.view addGestureRecognizer:pinch];
}
这是我的每个手势的方法,我的点按和长按工作就好了。
-(void)tapReconginzed:(UIGestureRecognizer *)sender{
NSString *output = [NSString stringWithFormat:@"tapped at %f", [sender locationInView:self.view].x];
[_tapped setText:output];
}
-(void)longPressReconginzed:(UIGestureRecognizer *)sender{
NSString *output = [NSString stringWithFormat:@"Long pressed at %f", [sender locationInView:self.view].x];
[_tapped setText:output];
}
-(void)rotateReconginzed:(UIRotationGestureRecognizer *)sender{
if ([sender state] == UIGestureRecognizerStateBegan || [sender state] == UIGestureRecognizerStateChanged) {
CGPoint locationOne = [sender locationOfTouch:0 inView:self.view];
NSString *output = [NSString stringWithFormat:@"rotated at %f and %f", locationOne.x, locationOne.y];
[_tapped setText:output];
}
}
-(void)pinchReconginzed:(UIPinchGestureRecognizer *)sender{
if ([sender state] == UIGestureRecognizerStateBegan || [sender state] == UIGestureRecognizerStateChanged) {
CGPoint locationOne = [sender locationOfTouch:0 inView:self.view];
NSString *output = [NSString stringWithFormat:@"Pinched pressed at %f and %f", locationOne.x, locationOne.y];
[_tapped setText:output];
}
if (sender.state == UIGestureRecognizerStateEnded){
CGPoint locationOne = [sender locationOfTouch:0 inView:self.view];
NSString *output = [NSString stringWithFormat:@"pinched ended at %f and %f",locationOne.x, locationOne.y];
[_tapped setText:output];
}
}
任何帮助将不胜感激!谢谢!
答案 0 :(得分:1)
他们没有认出,或者只是没有出现在标签上吗?您有多个手势识别器尝试在同一标签中设置文本,因此您可能无法看到所有更新。尝试使用NSLog
查看手势是否实际被识别。
如果不是,您可能还想尝试设置捏合并将手势“delegate
”设置为self
,然后从YES
返回-gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:
。
更新:以消除冲突的可能性,尝试除了捏之外的所有注释,或者除了旋转之外的所有注释,并查看它们是否可以自行工作。如果没有,您可能配置错误。让他们单独工作,然后再尝试让他们一起工作。