我有以下问题。我正在使用UILongPressGestureRecognizer将UIView设置为“切换模式”。如果UIView处于“切换模式”,则用户能够在屏幕上拖动UIView。为了在屏幕上拖动UIView,我使用了touchesBegan、touchesMoved和touchesEnded方法。
它可以工作,但是:我必须抬起手指才能拖动它,因为touchesBegan方法已经被调用,因此不会再次调用,因此我不能在屏幕上拖动UIView。
在UILongPressGestureRecognizer被触发后,有没有办法手动调用touchesBegan (UILongPressGestureRecognizer更改BOOL值,只有当BOOL值设置为YES时,touchesBegan才起作用)。
发布于 2013-02-06 21:42:52
UILongPressGestureRecognizer是一个连续的手势识别器,所以不要求助于touchesMoved或UIPanGestureRecognizer,只需检查UIGestureRecognizerStateChanged,例如:
- (void)viewDidLoad
{
[super viewDidLoad];
UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[self.view addGestureRecognizer:gesture];
}
- (void)handleGesture:(UILongPressGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:gesture.view];
if (gesture.state == UIGestureRecognizerStateBegan)
{
// user held down their finger on the screen
// gesture started, entering the "toggle mode"
}
else if (gesture.state == UIGestureRecognizerStateChanged)
{
// user did not lift finger, but now proceeded to move finger
// do here whatever you wanted to do in the touchesMoved
}
else if (gesture.state == UIGestureRecognizerStateEnded)
{
// user lifted their finger
// all done, leaving the "toggle mode"
}
}发布于 2013-02-06 21:38:44
我建议你使用UIPanGestureRecognizer,因为它是一种推荐的拖拽手势。
maximumNumberOfTouches
minimumNumberOfTouches
- (void)setTranslation:(CGPoint)translation inView:(UIView *)view
示例:
1. You have to use a global variable to retain the old frame. Get this in UIGestureRecognizerStateBegan.
2. When the state is UIGestureRecognizerStateChanged. You can use the-(void) pannningMyView:(UIPanGestureRecognizer*) panGesture{ if(panGesture.state==UIGestureRecognizerStateBegan){ //保留原始状态}else if(panGesture.state==UIGestureRecognizerStateChanged){ translatedPoint=panGesture translationInView:self.view;//在这里你可以设法获得新的拖动点。}}
- (CGPoint)velocityInView:(UIView *)view
https://stackoverflow.com/questions/14730056
复制相似问题