UISwipeGestureRecognizer不工作?

时间:2010-12-30 08:29:33

标签: iphone ipad uigesturerecognizer

我在iPad中使用swipeEvent(使用UISwipeGestureRecognizer)时使用以下代码来旋转视图。但是当我连续进行Swipping(2或3)时,方法未被调用,我知道原因,我使用的是Duration在CABasicAnimation的那个方法中。我怎么能克服这个问题?请帮忙吗?

-(void)handleSwipeFromD:(UISwipeGestureRecognizer *)recognizer
{
 NSLog(@"down");
if (operationStart)
{

self.commonLayer = [self.layer presentationLayer];
self.layer.transform = commonLayer.transform;
[self.commonLayer removeAnimationForKey:@"rotationAnimate"];

self.commonLayer = self.layer;
rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.fromValue = [NSNumber numberWithFloat:0.0 * M_PI];
rotationAnimation.toValue = [NSNumber numberWithFloat:2.0 * M_PI];
//rotationAnimation.toValue = [NSNumber numberWithFloat: 2.5 * 3.15 ];
rotationAnimation.duration = 1.0;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1.0; 
//rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
[self.commonLayer addAnimation:rotationAnimation forKey:@"rotationAnimate"];

  }  
}

1 个答案:

答案 0 :(得分:3)

以下内容放置在视图的控制器中,而不是视图中,很多次都像魅力一样。

- (void)viewDidLoad {
    [super viewDidLoad];
    UISwipeGestureRecognizer *sw = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromD:)];
    [self.view addGestureRecognizer:sw];
    [sw release];
}


-(void)handleSwipeFromD:(UISwipeGestureRecognizer *)recognizer
{
    BOOL operationStart = YES;
    if (operationStart)
    {
        //self.view.la
    //  self.view.commonLayer = [self.layer presentationLayer];
    //  self.view.layer.transform = commonLayer.transform;
        [self.view.layer removeAnimationForKey:@"rotationAnimate"];

        CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        rotationAnimation.fromValue = [NSNumber numberWithFloat:0.0 * M_PI];
        rotationAnimation.toValue = [NSNumber numberWithFloat:2.0 * M_PI];
        //rotationAnimation.toValue = [NSNumber numberWithFloat: 2.5 * 3.15 ];
        rotationAnimation.duration = 1.0;
        rotationAnimation.cumulative = YES;
        rotationAnimation.repeatCount = 1.0; 
        //rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
        [self.view.layer addAnimation:rotationAnimation forKey:@"rotationAnimate"];

    }  
}

然而,由于我删除了所有“commonLayer”引用,因此它不具有相同的层次结构,而且我并没有真正从代码中获取它所扮演的角色。