0

我想展示一个带有旋转对象图像的动画。我有一个NSArray对象的框架,我想用属性逐帧显示它们UIImageView animationImages

问题是我想用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];

}
4

1 回答 1

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));
于 2013-04-16T15:37:09.147 回答