gpt4 book ai didi

ios - 如何让UIView动画更流畅?

转载 作者:行者123 更新时间:2023-11-29 04:04:50 25 4
gpt4 key购买 nike

我有几个手势识别器来左右滑动图像或 vv,具体取决于滑动方向。但我希望动画更流畅。这是我到目前为止的代码:

-(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? ),所以我切换到手势。但如果您看到他们的应用程序,滑动速度会更快。

最佳答案

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

更新

我这样修改了我的代码。与往常一样,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

关于ios - 如何让UIView动画更流畅?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15423868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com