UIView 块动画不起作用
UIView block animation not working
我有一个自定义视图(绘制 callouts/bubbles),我目前将其作为子视图添加到 UIImageView。它按预期工作,但我想用类似 spring 的效果为操作设置动画。我正在使用下面的代码,但它不起作用(块被执行但没有动画):
/* ViewController */
UIImageView *iv = self.imageView;
ZGCBubbleView *bubbleView = [[ZGCBubbleView alloc] init];
bubbleView.hidden = YES;
[iv addSubview:bubbleView];
// UIView animation test
[UIView animateWithDuration:2.0
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0.5
options:UIViewAnimationOptionAllowAnimatedContent & UIViewAnimationOptionLayoutSubviews
animations:^{
bubbleView.hidden = NO;
[self.view layoutIfNeeded]; // tried this
}
completion:nil];
我尝试为隐藏的 属性 和 alpha 属性设置动画,但结果相同。我也试过动画 addSubview 方法,没有区别。我从一个更简单的动画开始作为概念证明,但这也不起作用。
以上代码在 (void)viewDidAppear:(BOOL)animated 循环期间调用的方法中执行。这与动画在主线程期间执行有什么关系吗?我读过一些类似的东西但不确定。此外,我正在为 UIImageView 使用自动布局,但认为在这种情况下这并不重要,因为动画正在应用于 UIImageView 的自定义子视图。
感谢任何帮助。
试试这个。
UIButton *catchButton = (UIButton *)sender;
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut//UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat |
animations:^{
catchButton.alpha = 0.4;
catchButton.enabled = NO;
}
completion:NULL];
最好使用 alpha。
UIImageView *iv = self.imageView;
ZGCBubbleView *bubbleView = [[ZGCBubbleView alloc] init];
bubbleView.alpha = 0;
// bubbleView.hidden = YES;
[iv addSubview:bubbleView];
// UIView animation test
[UIView animateWithDuration:2.0
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0.5
options:UIViewAnimationOptionAllowAnimatedContent & UIViewAnimationOptionLayoutSubviews
animations:^{
// bubbleView.hidden = NO;
bubbleView.alpha = 1;
//[self.view layoutIfNeeded]; // tried this
}
completion:nil];
我有一个自定义视图(绘制 callouts/bubbles),我目前将其作为子视图添加到 UIImageView。它按预期工作,但我想用类似 spring 的效果为操作设置动画。我正在使用下面的代码,但它不起作用(块被执行但没有动画):
/* ViewController */
UIImageView *iv = self.imageView;
ZGCBubbleView *bubbleView = [[ZGCBubbleView alloc] init];
bubbleView.hidden = YES;
[iv addSubview:bubbleView];
// UIView animation test
[UIView animateWithDuration:2.0
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0.5
options:UIViewAnimationOptionAllowAnimatedContent & UIViewAnimationOptionLayoutSubviews
animations:^{
bubbleView.hidden = NO;
[self.view layoutIfNeeded]; // tried this
}
completion:nil];
我尝试为隐藏的 属性 和 alpha 属性设置动画,但结果相同。我也试过动画 addSubview 方法,没有区别。我从一个更简单的动画开始作为概念证明,但这也不起作用。
以上代码在 (void)viewDidAppear:(BOOL)animated 循环期间调用的方法中执行。这与动画在主线程期间执行有什么关系吗?我读过一些类似的东西但不确定。此外,我正在为 UIImageView 使用自动布局,但认为在这种情况下这并不重要,因为动画正在应用于 UIImageView 的自定义子视图。
感谢任何帮助。
试试这个。
UIButton *catchButton = (UIButton *)sender;
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut//UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat |
animations:^{
catchButton.alpha = 0.4;
catchButton.enabled = NO;
}
completion:NULL];
最好使用 alpha。
UIImageView *iv = self.imageView;
ZGCBubbleView *bubbleView = [[ZGCBubbleView alloc] init];
bubbleView.alpha = 0;
// bubbleView.hidden = YES;
[iv addSubview:bubbleView];
// UIView animation test
[UIView animateWithDuration:2.0
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0.5
options:UIViewAnimationOptionAllowAnimatedContent & UIViewAnimationOptionLayoutSubviews
animations:^{
// bubbleView.hidden = NO;
bubbleView.alpha = 1;
//[self.view layoutIfNeeded]; // tried this
}
completion:nil];