【问题标题】:Can't we use touchesBegan,touchesMoved,touchesEnded for a scrollview?我们不能使用 touchesBegan、touchesMoved、touchesEnded 作为滚动视图吗?
【发布时间】:2010-04-26 12:56:56
【问题描述】:
实际上我有一个滚动视图。我使用的是 30 个按钮。我的要求是我需要重新排列按钮。就像,当我触摸任何按钮时,应该用我们的触摸来选择它。并且无论我们在滚动视图中移动到哪里,它都应该随着我们的触摸而移动。在我结束触摸后,应该交换按钮。任何人都可以帮助我解决这个问题............
【问题讨论】:
标签:
objective-c
uiscrollview
touch
【解决方案1】:
您可以执行此操作,但需要做很多工作。您需要下一个:
1. 创建 UIControl 子类作为您的按钮。
2. 覆盖所有的 touches* 方法。
3.实现对标准uicontrol行为+移动行为的支持。
#pragma mark -
#pragma mark Touches delegate methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
[self performSelector:@selector(delayedTouchBeging:) withObject:touch afterDelay:0.15];
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ( dragging ) {
// move your view in new position here
} else {
[super touchesMoved:touches withEvent:event];
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ( dragging ) {
// Do other stuff if you need
} else {
[super touchesEnded:touches withEvent:event];
}
dragging = NO;
[NSObject cancelPreviousPerformRequestsWithTarget:self];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesEnded:touches withEvent:event];
}
- (void) delayedTouchBeging:(UITouch*) touch {
dragging = YES;
[self cancelTrackingWithEvent:nil];
// Do here stuff about begin moving (animation, etc)
}