【问题标题】:Why UIView animation doesn't work为什么 UIView 动画不起作用
【发布时间】:2012-08-31 06:59:18
【问题描述】:

我要做的是以编程方式在屏幕的左上角创建一个 UIView 矩形,然后将其移动到右上角、右下角、左下角,最后回到左上角。但它没有按预期工作。我的代码有什么问题?

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,strong) UIView *myView;
@end

@implementation ViewController
@synthesize myView = _myView;


- (IBAction)animation:(UIButton *)sender {
    [UIView animateWithDuration:3.0 animations:^{
        self.myView.alpha = 0.75;
        self.myView.frame = CGRectMake(160, 0, 160,230);}];

    [UIView animateWithDuration:3.0 animations:^{
        self.myView.alpha = 0.50;
        self.myView.frame = CGRectMake(160, 230, 160,230);}];

     [UIView animateWithDuration:3.0 animations:^{
     self.myView.alpha = 0.25;
     self.myView.frame = CGRectMake(0, 230, 160,230);}];

     [UIView animateWithDuration:3.0 animations:^{
     self.myView.alpha = 0.00;
     self.myView.frame = CGRectMake(0, 0, 160,230);}
     completion:^(BOOL finished) {
     [self.myView removeFromSuperview];
     }];

}


- (void)viewDidLoad
{
    [super viewDidLoad];
    CGRect viewRect = CGRectMake(0, 0, 160, 230);
    UIView *mv = [[UIView alloc] initWithFrame:viewRect];
    self.myView = mv;
    self.myView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.myView];

}
@end

编辑: 我解决了嵌套完成块的问题:

- (IBAction)animation:(UIButton *)sender {
    [UIView animateWithDuration:3.0 animations:^{
        self.myView.alpha = 0.75;
        self.myView.frame = CGRectMake(160, 0, 160,230);}
     completion:^(BOOL finished) {
         [UIView animateWithDuration:3.0 animations:^{
             self.myView.alpha = 0.50;
             self.myView.frame = CGRectMake(160, 230, 160,230);}
          completion:^(BOOL finished) {
              [UIView animateWithDuration:3.0 animations:^{
                  self.myView.alpha = 0.25;
                  self.myView.frame = CGRectMake(0, 230, 160,230);}
          completion:^(BOOL finished) {
              [UIView animateWithDuration:3.0 animations:^{
                  self.myView.alpha = 0.00;
                  self.myView.frame = CGRectMake(0, 0, 160,230);}
                               completion:^(BOOL finished) {
                                   [self.myView removeFromSuperview];
          }];}];}];}];}

但读起来很糟糕。还有其他方法吗?

【问题讨论】:

  • 尝试当前部分完成后开始动画的下一部分,而不是与其并行。穷人UIView 无法决定应该做什么。 :)
  • 听起来很有趣.. 你能更具体一点,把代码放在答案里吗?

标签: objective-c


【解决方案1】:

这不是具体的动画,但您应该尝试这种模式以使动画的各个部分依次启动:

[UIView animateWithDuration:3.0 animations:^{
    // first part of the animation
} completion:^(BOOL finished) {
    [UIView animateWithDuration:3.0 animations:^{
        // second part of animation
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:3.0 animations:^{
            // third part of the animation
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:3.0 animations:^{
                // forth part of the animation
            } completion:^(BOOL finished) {
                // finish and clear the animation
            }];
        }];
    }];
}];

【讨论】:

  • 握手。我刚发现一样。但是嵌套块看起来很难看。还有什么办法吗?
  • 你可以用finish-selectors制作没有块的动画,但是Apple强烈推荐iOS4+中的基于块的动画。
猜你喜欢
  • 2011-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多