我正在尝试创建一个应用程序,当UILongPressGestureRecognizer手势被触发时,UIButtons可以被拖放。实际上我的应用程序在任何iPad上运行良好。它仅在iPhone中产生问题,并且低于iOS 5.0,因此在使用iOS 5.0的iPhone中工作得很好。
UILongPressGestureRecognizer * gesture = [[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(moveActionGestureRecognizerStateChanged:)];
gesture.minimumPressDuration = 0.5;
gesture.delegate = self;
[self.dragView addGestureRecognizer: gesture];
[gesture release];
- (void) moveActionGestureRecognizerStateChanged: (UILongPressGestureRecognizer *) recognizer
{
switch ( recognizer.state )
{
default:
case UIGestureRecognizerStateFailed:
{
dragView.alpha=1.0;
[dragView release];
dragView=nil;
break;
}
case UIGestureRecognizerStatePossible:
{
dragView.alpha=0.8;
dragView.frame=CGRectMake(dragView.frame.origin.x, dragView.frame.origin.y, dragView.frame.size.width, dragView.frame.size.height);
}
case UIGestureRecognizerStateCancelled:
{
dragView.alpha=1.0;
[dragView release];
dragView=nil;
break;
}
case UIGestureRecognizerStateEnded:
{
//Set dragView on target position
break;
}
case UIGestureRecognizerStateBegan:
{
//NSLog(@"Began");
dragView.alpha=0.8;
dragView.frame=CGRectMake(dragView.frame.origin.x, dragView.frame.origin.y, dragView.frame.size.width, dragView.frame.size.height);
[self bringSubviewToFront:dragView];
break;
}
case UIGestureRecognizerStateChanged:
{
[self.view bringSubviewToFront:dragView];
CGPoint offset = [recognizer locationInView: self.scrollView];
dragView.frame=CGRectMake(offset.x, offset.y, dragView.frame.size.width, dragView.frame.size.height);
}
break;
}
}
我有2台设备iPhone 3G与iOS 4.2.1和iPhone 4与5.0。此功能在带有iOS 5.0的iPhone 4中运行良好,但在带有iOS 4.2.1的iPhone 3g中无法正常工作。有时它在iPhone 3g中工作,但有时它不会调用委托方法。
如果您有任何解决方案,请告诉我。
谢谢!
答案 0 :(得分:2)
是否根本没有调用委托方法,或者没有选择案例?我不知道这是否真的解决了你的问题,但你应该修复你的switch
(在默认情况下抢劫):
switch ( recognizer.state )
{
case someCase:
{
// ...
break;
}
default:
break;
}