gpt4 book ai didi

ios - UIView 动画 block 暂停动画和完成代码

转载 作者:可可西里 更新时间:2023-11-01 03:31:50 25 4
gpt4 key购买 nike

我有一个动画 block 来执行一个简单的基于变换的动画,完成后从其父 View 中删除有问题的 View 。

UIView *msgView = [[UIView alloc] initWithFrame:CGRectMake(160, 120, 160, 100)];

// Do stuff to set up the subviews of msgView.

// Add the msgView to the superview (ViewController) that is going to display it.

CATransform3D transform = CATransform3DMakeScale(2.5, 2.5, 1.0);

[UIView animateWithDuration:5.0
animations:^(void){msgView.layer.transform = transform;}
completion:^(BOOL finished){[msgView removeFromSuperview];}];

然后我使用技术问答 1673 http://developer.apple.com/library/ios/#qa/qa1673/_index.html 中详述的代码暂停动画。

-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}

-(void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}

但是,这段代码不会阻止完成代码的执行。因此,为了防止代码执行,我将完成代码更改为:

completion:^(BOOL finished){if(finished == TRUE)[msgView removeFromSuperview];};

检查 finished == TRUE 可防止在动画 block 暂停时执行完成代码。如果在您“取消暂停”动画之前超过了持续时间,则不会执行完成代码。即在这种情况下,msgView 保留在 super View 中。

是否有暂停/取消暂停与完成代码关联的动画和计时器(如果这是正在发生的事情)?

最佳答案

正如我在上面的评论中所说,似乎没有问题。下面是在 iOS 5.1 和 6.1 中测试的。

使用 UIImageView *transViewUIButton *trigger 创建 Storyboard。这是类(class):

TSTViewController.h:

@property (weak, nonatomic) IBOutlet UIImageView *transView;
@property (weak, nonatomic) IBOutlet UIButton *trigger;
@property (nonatomic) NSUInteger bState;

- (IBAction)didPressTrigger:(id)sender;

TSTViewController.m:

- (void)viewDidLoad
{
[super viewDidLoad];
self.bState = 0; // 0 is initial state
// 1 is transform being animated
// 2 is transform paused
// 3 is transform ended
}

- (IBAction)didPressTrigger:(id)sender {
switch (self.bState) {
case 0:
{
CATransform3D transform = CATransform3DMakeScale(2.5, 2.5, 1.0);
self.bState++;
[UIView animateWithDuration:5.0
animations:^(void){self.transView.layer.transform = transform;}
completion:^(BOOL finished){
self.bState = 3;
NSLog(@"Done");
}];
break;
}
case 1:
{
self.bState++;
[self pauseLayer:self.transView.layer];
break;
}
case 2:
{
self.bState = 1;
[self resumeLayer:self.transView.layer];
break;
}
case 3:
{
[UIView animateWithDuration:0 animations:^(void){self.transView.layer.transform = CATransform3DIdentity;}
completion:^(BOOL finished) {
self.bState = 0;
NSLog(@"Reset");
}];
break;
}
default:
break;
}
}

-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}

-(void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}

当您按下触发按钮时,动画开始。再按一次,动画停止。等待 10 秒钟,然后再次按下按钮。动画继续并结束,并记录“完成”。

关于ios - UIView 动画 block 暂停动画和完成代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15119839/

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