gpt4 book ai didi

iphone - CAAnimation 的问题

转载 作者:可可西里 更新时间:2023-11-01 04:42:16 26 4
gpt4 key购买 nike

//卡片类的头文件

#import "OWCardView.h"

@implementation OWCardView;

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.layer.contents = (id) [UIImage imageNamed:@"4.png"].CGImage;
self.layer.shadowColor = [UIColor whiteColor].CGColor;
self.layer.shadowOpacity = 0.3;
}
return self;
}


- (void) moveCard:(float)xPoint along:(float)yPoint
{
CGRect someRect = CGRectMake(xPoint, yPoint, 72, 96);

[UIView animateWithDuration: 3.5
delay: 0.3
options: UIViewAnimationCurveEaseOut
animations: ^{ self.frame = someRect;}
completion: ^(BOOL finished) { }];
}


@end


// part of the Implementation in the view controller's viewDidLoad

[...]
CGRect myImageRect = CGRectMake(20.0f, 20.0f, 72.0f, 96.0f);

// myCard is a property of the viewController
self.myCard = [[OWCardView alloc] initWithFrame:myImageRect];
[self.view addSubview:self.myCard];
[self.myCard moveCard: 240 along: 355]; // First call
[self.myCard moveCard: 50 along: 355]; // Second call
[self.myCard moveCard: 50 along: 50]; // Third call
[...]

问题:只执行viewController中moveCard的最后一条消息;第一次和第二次调用不执行。代码有什么问题?

当我将第三条消息评论到 moveCard 时,第二个调用被执行而第一个没有。如果我评论第二个和第三个调用,那么第一个调用就会执行。但我希望所有三个动画都出现在卡片上。

最佳答案

当您为 View 设置动画时,您之前应用到该 View 的任何动画都会立即取消,即使任一动画具有非零延迟也是如此。所以如果你想应用顺序动画,你必须在第一个动画结束后添加第二个动画。您可以使用完成 block 来检测第一个动画何时结束。

很容易让 moveCard:along: 维护一个目标矩形队列并按顺序应用动画:

@implementation OWCardView {
NSMutableArray *pendingRects;
BOOL isAnimating;
}

- (void)moveCard:(float)xPoint along:(float)yPoint {
CGRect someRect = CGRectMake(xPoint, yPoint, 72, 96);
if (!pendingRects) {
pendingRects = [NSMutableArray array];
}
[pendingRects addObject:[NSValue valueWithCGRect:someRect]];
[self startAnimatingIfNeeded];
}

- (void)startAnimatingIfNeeded {
if (isAnimating)
return;
if (pendingRects.count == 0)
return;
CGRect rect = [[pendingRects objectAtIndex:0] CGRectValue];
[pendingRects removeObjectAtIndex:0];

isAnimating = YES;
[UIView animateWithDuration:3.5 delay:0.3 options:UIViewAnimationCurveEaseOut animations:^{
self.frame = rect;
} completion:^(BOOL finished) {
isAnimating = NO;
[self startAnimatingIfNeeded];
}];
}

关于iphone - CAAnimation 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13949295/

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