CokileCeoi CokileCeoi - 6 months ago 21
iOS Question

animation not works in the non-initial UIViewController

I have a

UIView
called
loadingView
shows some
UIActivityIndicatorView
-like animation. There are two
UIViewController
s that all have such
UIView
.

The initial
UIViewController
's code:

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


Also it has a button to present the second
UIViewController
:

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


The second
ViewController
's
viewDidLoad
method code:

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


As you can see, the two
UIViewController
s has the same
viewDidLoad
method code.

The
loadingView
works well in the initial
UIViewController
.

But in the second
UIViewController
, the
loadingView
only displays the black frame( I set the background colour to black).

Here is my
loadingView
implement code:

#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;
}

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

Put this code in viewDidAppear of SecondViewController so it works in present mode as well.

Comments