简体   繁体   中英

addsubview animation works only first time

I am trying add subview with animation effect using the code which work fine for the first time below.

CATransition *transition = [CATransition animation];
transition.duration = 1.0;
transition.type = kCATransitionMoveIn; 
transition.subtype = kCATransitionFromRight;

[newView.layer removeAllAnimations]
[newView.layer addAnimation:transition forKey:nil];
[self.masterview addSubview:newView];

There is a back button in newView which removes the View from the superview.

[newView removeFromSuperview];

Now when I try adding newView as subview again using the above code,its first adds the view as a subview(without animation) and again with animation.

Instead of removing newView from superview, why not you can just set a frame like

newView.frame = CGRectMake(0, -500, CGRectGetWidth(newView.frame), CGRectGetHeight(newView.frame));

so that you can just hide it from showing. Just an idea. :-)

When you remove the view from its super view it keeps its frame as it is. So, i think you have to change the newView location to be placed in the new location again.

It will work fine for all the times.But the only difference you have to find is that you need to wai for the duration of animation. It will not animate continuously. It will take a minimum time that you mentioned in the duration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM