我想展示一个旋转物体图像的动画。
我有一个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];
}
答案 0 :(得分: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));