如何使UIView动画更流畅?

时间:2013-03-15 02:50:43

标签: ios animation uiview

根据滑动的方向,我有几个手势识别器可左右滑动图像或向右滑动图像。但我希望动画更顺畅。到目前为止,这是我的代码:

-(void)swipeNext:(id)sender{
    //0 Get the currentview as referenced by pageControl
    UIImageView *oldView = [views objectAtIndex:currentViewIndex];
    //MOVE ON TO NEW VIEW
    currentViewIndex ++;
    NSLog(@"left swiped  - currentViewIndex:%d", currentViewIndex);
    UIImageView *newView = [views objectAtIndex:currentViewIndex];
    //NSLog(@"views objectAtIndex = %@",[views objectAtIndex:[self.pageControl currentPage]]);

    //1 Animate out the old view
    newView.frame = oldView.frame;
    newView.center = CGPointMake(oldView.center.x + CGRectGetWidth(oldView.frame), oldView.center.y);
    [oldView.superview addSubview: newView];

    [UIView animateWithDuration:1.0
                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseIn //UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
                     animations:^{
                         newView.center = oldView.center;
                         oldView.center = CGPointMake(oldView.center.x - CGRectGetWidth(oldView.frame), oldView.center.y);

                     }
                     completion:nil];
}

我想让它快一点。我发布了作为我试图模仿的应用程序的参考,DunkinDonuts(What kind of viewcontrollers does the Dunkin Donuts app use),他们似乎使用了UIPageControl,我最初尝试使用它,但无法确定滑动的方向(How do I animate a UIImageView left to right with UIPageControl? ),所以我切换到手势。但是如果你看到他们的应用程序,那么滑动就会更快。

2 个答案:

答案 0 :(得分:1)

我发现要添加格式正确(可读)的代码块,我无法使用注释。有人建议我使用答案作为原始问题的更新。所以就是这样。

更新

我修改了这样的代码。与往常一样,viewDidLoad调用createUIViews执行此操作:

-(void)createUIViews{
    UIImage *image1 = [UIImage imageNamed:@"GiftCard.png"];
    firstView = [[UIImageView alloc] initWithImage:image1];

    CGRect newFrame1 = firstView.frame;
    newFrame1.origin.x = 50;
    newFrame1.origin.y = 10;
    firstView.frame = newFrame1;

    UIImage *image2 = [UIImage imageNamed:@"MyCard.png"];
    secondView = [[UIImageView alloc] initWithImage:image2];

    CGRect newFrame2 = secondView.frame;
    newFrame2.origin.x = 350;//CGRectGetWidth(firstView.superview.frame)/2;
    newFrame2.origin.y = 10;
    secondView.frame = newFrame2;

    //Add all Views offscreen except #1
    [self.scrollView addSubview:firstView]; //added ONSCREEN
    [self.scrollView addSubview:secondView]; //added OFFSCREEN
}

这会将第一个View设置在屏幕上,将第二个View设置在右侧。

手势识别器也在viewDidLoad中设置。他们调用swipeNext,如下所示:

-(void)swipeNext:(id)sender{
//0 Get the currentview as referenced by pageControl
oldView = [views objectAtIndex:currentViewIndex];
//MOVE ON TO NEW VIEW
currentViewIndex ++;
newView = [views objectAtIndex:currentViewIndex];

//1 Animate out the old view **(Aaron, youre right, dont need this anymore)**
//newView.frame = oldView.frame;
//newView.center = CGPointMake(oldView.center.x + CGRectGetWidth(oldView.frame), oldView.center.y);
//[oldView.superview addSubview: newView];

[UIView animateWithDuration:1.0
                      delay: 0.0
                    options: UIViewAnimationOptionCurveEaseIn 
                 animations:^{
                     newView.center = oldView.center;
                     oldView.center = CGPointMake(oldView.center.x - CGRectGetWidth(oldView.frame), oldView.center.y);

                 }
                 completion:nil];

} 我注释掉了addSubview,因为我把它移到了createUIViews方法。它的工作方式相同,我认为它只是一个快速或一些我想念的动画功能的问题。我希望我能添加一个视频。原始的DD应用程序看起来更快,因为UIPageControl(我认为)这是一个视频的链接。Sluggish iOS UIView Animation

答案 1 :(得分:0)

在事件循环的同一转弯期间向视图层次结构添加视图,以启动同一视图的动画,这是性能不佳和其他奇怪的动画故障的一个秘诀。在您的代码中,您知道在调用swipeNext之前newView将是什么。它是[views objectAtIndex:currentViewIndex + 1]。我建议您在当前视图完成动画制作到位时将newView(以及之前的视图)添加到视图层次结构中。例如,在视图层次结构中放置“nextView”和“previousView”的代码可以放在动画的完成块中。将nextView和previousView提前放在视图层次结构中,但是在屏幕外。然后在swipeToNext中,您可以为中心设置动画,并避免在动画完成之前修改视图层次结构。