我有这个方法,当用UIPanGestureRecognizer拖动我的按钮时调用:
- (void)wasDragged:(UIPanGestureRecognizer *)recognizer {
UIButton *button = (UIButton *)[recognizer view];
CGPoint translation = [recognizer translationInView:button];
button.center = CGPointMake(button.center.x + translation.x, button.center.y + translation.y);
[recognizer setTranslation:CGPointZero inView:button];
if(recognizer.state == UIGestureRecognizerStateEnded) {
for (UIView *field in self.fieldsArray) {
CGPoint fieldCenter = [field convertPoint:field.center toView:self.view];
if (CGRectContainsPoint (button.frame, fieldCenter)) {
NSLog(@"done");
break;
} else {
NSLog(@"nope");
}
}
}
}
我的self.fieldsArray
以其他方式创建:
for (int i = 0; i < quantity; i++) {
UIView *field = [[UIView alloc] initWithFrame:CGRectMake(i * (widthOfField + 3), 0, widthOfField, 50 )];
[field setBackgroundColor:[UIColor colorWithRed:70/255 green:70/255 blue:70/255 alpha:1.0]];
[field.layer setShadowOffset:CGSizeMake(2, 2)];
[field.layer setShadowColor:[[UIColor blackColor] CGColor]];
[field.layer setShadowOpacity:0.8];
[field.layer setCornerRadius:10];
[self.fieldsArray addObject:field];
[self.fieldsView addSubview:field];
}
问题是:为什么当我将按钮拖到1号或3号或5号等字段时,我有“完成”消息。当我将按钮拖到2或4或6等字段时,我有“不”?
如果我删除break;
:
例如,当我拖动字段编号5(以及我的所有6个)上的按钮时,我有日志:nope, nope, done, nope, nope, nope
在第3号字段:nope, done, nope, nope, nope, nope