【问题标题】:animation not works in the non-initial UIViewController动画在非初始 UIViewController 中不起作用
【发布时间】:2016-09-20 04:05:55
【问题描述】:

我有一个名为loadingViewUIView 显示了一些类似UIActivityIndicatorView 的动画。有两个UIViewControllers 都有这样的UIView

UIViewController的初始代码:

- (void)viewDidLoad {
    self.loadingView = [[LoadingView alloc] initWithFrame:CGRectMake(10, 1, 120, 120)];
    [self.view addSubview:self.loadingView];
}

它还有一个按钮来显示第二个UIViewController

- (void)buttonPressed {
    SecondViewController *sVC = [[SecondViewController alloc] init];
    [self presentViewController:sVC animated:YES completion:nil];
}

第二个ViewControllerviewDidLoad方法代码:

- (void)viewDidLoad {
    self.loadingView = [[LoadingView alloc] initWithFrame:CGRectMake(10, 1, 120, 120)];
    [self.view addSubview:self.loadingView];
}

如您所见,两个UIViewControllers 具有相同的viewDidLoad 方法代码。

loadingView 在初始 UIViewController 中运行良好。

但在第二个UIViewControllerloadingView 只显示黑框(我将背景颜色设置为黑色)。

这是我的loadingView 实现代码:

#define NUMBER_OF_DOT 15
#define DURATION 1.5

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];

    if (self) {
        self.replicatorLayer = [[CAReplicatorLayer alloc] init];
        self.replicatorLayer.frame = frame;
        self.replicatorLayer.cornerRadius = 10.0;
        self.replicatorLayer.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.86].CGColor;
        self.replicatorLayer.position = self.center;
        self.replicatorLayer.instanceDelay = DURATION/NUMBER_OF_DOT;
        [self.layer addSublayer:self.replicatorLayer];

        float size = frame.size.width*14/200;
        self.dot = [[CALayer alloc] init];
        self.dot.bounds = CGRectMake(0, 0, size, size);
        self.dot.position = CGPointMake(frame.size.width/2, frame.size.height/5);
        self.dot.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0].CGColor;
        self.dot.borderColor = [UIColor whiteColor].CGColor;
        self.dot.borderWidth = 1.0;
        self.dot.cornerRadius = 1.5;
        self.dot.transform = CATransform3DMakeScale(0.01, 0.01, 0.01);    
        [self.replicatorLayer addSublayer:self.dot];

        self.replicatorLayer.instanceCount = NUMBER_OF_DOT;
        float angle = 2*M_PI/NUMBER_OF_DOT;
        self.replicatorLayer.instanceTransform = CATransform3DMakeRotation(angle, 0.0, 0.0, 0.1);

        self.shrink = [[CABasicAnimation alloc] init];
        self.shrink.keyPath  = @"transform.scale";
        self.shrink.fromValue = [NSNumber numberWithFloat:1.0];
        self.shrink.toValue = [NSNumber numberWithFloat:0.1];
        self.shrink.duration = DURATION;
        self.shrink.repeatCount = INFINITY;

        [self.dot addAnimation:self.shrink forKey:nil];
    }

    return self;
}

【问题讨论】:

  • 您能否详细说明这一行 SecondViewController *sVC = [[HMPlaySceneViewController alloc] init];您分配 HMPlaySceneViewController 类型对象并提供 SecondViewController 类的引用。
  • @Shreyank 我的错。我已经更新了代码。 SecondViewController 就是 HMPlaySceneViewController。我重命名了HMPlaySceneViewController 以简化问题,但我忘了更新代码。
  • 我检查了你的代码。我在展示视图控制器时遇到了这个问题。但是当我尝试推送视图控制器时它工作正常。将检查并让您知道演示存在什么问题。

标签: ios objective-c iphone uiview uiviewcontroller


【解决方案1】:
-(void)viewDidAppear:(BOOL)animated
{
    self.loadingView = [[LoadingView alloc] initWithFrame:CGRectMake(10, 1, 120, 120)];
    [self.view addSubview:self.loadingView];
}

将此代码放在 SecondViewController 的 viewDidAppear 中,这样它也可以在当前模式下工作。

【讨论】:

  • 我也找到了原因。我不应该在viewDidLoad 中执行任何动画
  • 但是为什么动画在初始UIViewController时效果很好?
猜你喜欢
  • 2022-01-25
  • 2019-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-09
  • 1970-01-01
相关资源
最近更新 更多