2013-04-16 56 views
0

我想显示一个旋转对象的图像动画。 我有一个NSArray与对象的帧,我想逐帧显示它们的属性为UIImageViewanimationImages逐帧动画与UIGestureRecognizer

问题是我想用UISwipeGestureRecognizer(右和左)来控制动画。取决于手势的速度,对象应该旋转得更快或更慢。我认为这是不可能的,因为它被称为一次而不是连续的手势。

(注:这是我在StackOverflow上的第一个问题)

预先感谢您

编辑:我只是张贴我的解决方案。也许它对任何人都有用。我认为这很有用。

首先:向视图添加手势。

self.recognizer = [[UISwipeGestureRecognizer alloc] init]; 
self.recognizer.cancelsTouchesInView=NO; 
[self.view addGestureRecognizer:self.recognizer]; 

其次:在tocuhMoved方法我显示我取决于先前触摸的方向的需要的帧。它由用户的事件调用。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 


    NSInteger image= [self.animationView.animationImages indexOfObject:self.animationView.image]; 
    UITouch *touch = [touches anyObject]; 
    NSLog(@"Touch moved with index:%i", image); 

    if ((positionTouch.x<[touch locationInView:self.animationView].x)) { 
     if (image==24) { 
      self.animationView.image=[self.animationView.animationImages objectAtIndex:0]; 
     } 
     else{ 
      self.animationView.image=[self.animationView.animationImages objectAtIndex:image+1]; 
     } 
    } 
    else if((positionTouch.x>[touch locationInView:self.animationView].x)){ 
     if (image==0) { 
      self.animationView.image=[self.animationView.animationImages objectAtIndex:24]; 
     } 
     else{ 
      self.animationView.image=[self.animationView.animationImages objectAtIndex:image-1]; 
     } 

    } 


    positionTouch= [touch locationInView:self.animationView]; 
} 

不要忘记<UIGestureRecognizerDelegate>

用这种方法,我补帧的阵列...

-(void)addPictures{ 
    NSMutableArray* mArray=[[NSMutableArray alloc]initWithCapacity:25]; 
    for (int i=0; i<25; i++) { 
     [mArray insertObject:[UIImage imageNamed:[NSString stringWithFormat:@"Picture %i.jpg", i+1]] atIndex:i]; 
    } 

    self.animationView.image=[mArray objectAtIndex:0]; 
    self.animationView.animationImages=mArray; 


    [self.view addSubview:self.animationView]; 

} 

回答

0

这其实很简单,控制动画的速度。实际上,它非常简单(因为CALayer符合CAMediaTiming协议),控制它的属性字面上被称为speed。这只需要使用手势作为滑块的一种模拟,并计算相对于其在屏幕上的位置的x值。这应该是一个合适的起点:

//Speed changes necessitate a change in time offset, begin time, and 
//speed itself. Simply assigning to speed it not enough, as the layer needs 
//to be informed that it's animation timing function is being mutated. By 
//assigning a new time offset and a new begin time, the layer 
//automatically adjusts the time of any animations being run against it 
layer.timeOffset = [layer convertTime:CACurrentMediaTime() fromLayer:nil]; 
layer.beginTime = CACurrentMediaTime(); 
layer.speed = ([self.gesture locationInView:self.view].x/CGRectGetWidth(self.view.bounds)); 
+0

谢谢。我会尝试这个选项。 – TomCobo