Mik*_*e M 6 cocoa-touch objective-c uiviewanimation ios
我正在尝试UIView在收到按钮点击时执行此基本动画:
- (IBAction)buttonPress:(id)sender
{
self.sampleView.alpha = 0.0;
[UIView animateWithDuration:2.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState
animations:^{self.sampleView.alpha = 1.0;}
completion:NULL];
}
Run Code Online (Sandbox Code Playgroud)
在按钮之前单击视图是可见的,并且alpha值为1.0.当我发送按钮动作时,我希望视图在2秒内从alpha = 0.0淡入alpha = 1.0,但这不会发生.当我删除UIViewAnimationOptionBeginFromCurrentState动画工作正常.
看起来像UIViewAnimationOptionBeginFromCurrentState选项设置,没有设置alpha = 0.0并且跳过动画,因为它认为它已经是1.0.
我试图理解为什么会发生这种情况,因为Apple文档声明UIViewAnimationOptionBeginFromCurrentState如果另一个动画没有进行则没有效果:
" UIViewAnimationOptionBeginFromCurrentState
从与已在飞行中的动画相关联的当前设置开始动画.如果此键不存在,则允许在新动画开始之前完成任何飞行动画.如果另一个动画未在飞行中,则此键没有效果."
事实证明,使用UIViewAnimationOptionBeginFromCurrentState并不总是按预期工作。
看这个例子:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIButton *theButton = [UIButton new];
[self.view addSubview:theButton];
theButton.frame = self.view.frame;
theButton.backgroundColor = [UIColor redColor];
theButton.alpha = 0.05;
[theButton addTarget:self action:@selector(actionPressed:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)actionPressed:(UIButton *)theButton
{
theButton.alpha = 0.6;
[UIView animateWithDuration:5
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^
{
// you expect to animate from 0.6 to 1. but it animates from 0.05 to 1
theButton.alpha = 1;
}
completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,您希望.alpha从 0.6 动画到 1。但是,它会从 0.05 动画到 1。
为了解决该问题,您应该更改actionPressed:为以下内容:
- (void)actionPressed:(UIButton *)theButton
{
[UIView animateWithDuration:0
animations:^
{
// set new values INSIDE of this block, so that changes are
// captured in UIViewAnimationOptionBeginFromCurrentState.
theButton.alpha = 0.6;
}
completion:^(BOOL finished)
{
[UIView animateWithDuration:5
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^
{
// now it really animates from 0.6 to 1
theButton.alpha = 1;
}
completion:nil];
}];
}
Run Code Online (Sandbox Code Playgroud)
提一下animateWithDuration:0!!!
规则很简单:仅在UIViewAnimationOptionBeginFromCurrentState其他动画块之后使用动画块,以便实际应用您之前的所有更改。
如果您不知道animateWithDuration:0块中究竟应该包含什么,那么您可以使用以下技巧:
- (void)actionPressed:(UIButton *)theButton
{
// make all your changes outside of the animation block
theButton.alpha = 0.6;
// create a fake view and add some animation to it.
UIView *theFakeView = [UIView new];
theFakeView.alpha = 1;
[UIView animateWithDuration:0
animations:^
{
// we need this line so that all previous changes are ACTUALLY applied
theFakeView.alpha = 0;
}
completion:^(BOOL finished)
{
[UIView animateWithDuration:5
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^
{
// now it really animates from 0.6 to 1
theButton.alpha = 1;
}
completion:nil];
}];
}
Run Code Online (Sandbox Code Playgroud)
如果你不想记住这个 bug 的所有细节,那么只需将Method Swizzling应用到 UIView 类。
重要提示编辑:原来,下面的代码会导致崩溃在运行时,如果调用performBatchUpdates:completion:的UICollectionView实例。所以,我不建议method swizzling在这种情况下使用!
您的代码可能如下所示:
+ (void)load
{
static dispatch_once_t theOnceToken;
dispatch_once(&theOnceToken, ^
{
Class theClass = object_getClass(self);
SEL theOriginalSelector = @selector(animateWithDuration:delay:options:animations:completion:);
SEL theSwizzledSelector = @selector(swizzled_animateWithDuration:delay:options:animations:completion:);
Method theOriginalMethod = class_getClassMethod(theClass, theOriginalSelector);
Method theSwizzledMethod = class_getClassMethod(theClass, theSwizzledSelector);
if (!theClass ||!theOriginalSelector || !theSwizzledSelector || !theOriginalMethod || !theSwizzledMethod)
{
abort();
}
BOOL didAddMethod = class_addMethod(theClass,
theOriginalSelector,
method_getImplementation(theSwizzledMethod),
method_getTypeEncoding(theSwizzledMethod));
if (didAddMethod)
{
class_replaceMethod(theClass,
theSwizzledSelector,
method_getImplementation(theOriginalMethod),
method_getTypeEncoding(theOriginalMethod));
}
else
{
method_exchangeImplementations(theOriginalMethod, theSwizzledMethod);
}
});
}
+ (void)swizzled_animateWithDuration:(NSTimeInterval)duration
delay:(NSTimeInterval)delay
options:(UIViewAnimationOptions)options
animations:(void (^)(void))animations
completion:(void (^)(BOOL))completion
{
if (options & UIViewAnimationOptionBeginFromCurrentState)
{
UIView *theView = [UIView new];
theView.alpha = 1;
[UIView animateWithDuration:0
animations:^
{
theView.alpha = 0;
}
completion:^(BOOL finished)
{
[self swizzled_animateWithDuration:duration
delay:delay
options:options
animations:animations
completion:completion];
}];
}
else
{
[self swizzled_animateWithDuration:duration
delay:delay
options:options
animations:animations
completion:completion];
}
}
Run Code Online (Sandbox Code Playgroud)
如果您将此代码添加到您的自定义 UIView 类别,那么此代码现在可以正常工作:
- (void)actionPressed:(UIButton *)theButton
{
theButton.alpha = 0.6;
[UIView animateWithDuration:5
delay:0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^
{
// you expect to animate from 0.6 to 1.
// it will do so ONLY if you add the above code sample to your project.
theButton.alpha = 1;
}
completion:nil];
}
Run Code Online (Sandbox Code Playgroud)
就我而言,这个错误完全毁了我的动画。见下文:
[
] [
]
| 归档时间: |
|
| 查看次数: |
3378 次 |
| 最近记录: |