Note that there are some explanatory texts on larger screens.

plurals
  1. POiphone : remove CALayer when animation stop, CALayer flash before disappear
    primarykey
    data
    text
    <ul> <li>create a simple project in XCode</li> <li>set view to receive multi-touch events</li> <li>respond in touchesBegan, create CALayer when detect touch event</li> <li>make a opacity fade out animation for CALayer</li> <li>when animation stop, remove CALayer from parent</li> </ul> <p>Expect: CALayer disappear normally</p> <p>Actual: CALayer flash (blink) before disappear</p> <p>full source code:</p> <pre><code>@implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.multipleTouchEnabled = YES; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } - (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { for (UITouch* touch in touches) { CGPoint p = [touch locationInView:self.view]; //NSLog(@"touch=%@ p=%@", touch, NSStringFromCGPoint(p)); CALayer *layer = [CALayer layer]; layer.position = p; layer.bounds = CGRectMake(0, 0, 70, 70); layer.cornerRadius = 30; layer.masksToBounds = NO; layer.backgroundColor = [UIColor colorWithRed:102.0/255.0 green:156.0/255.0 blue:255.0/255.0 alpha:0.8].CGColor; layer.shouldRasterize = YES; CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"]; fadeOutAnimation.fromValue = [NSNumber numberWithFloat:1.0]; fadeOutAnimation.toValue = [NSNumber numberWithFloat:0.0]; fadeOutAnimation.duration = 0.5; fadeOutAnimation.delegate = self; fadeOutAnimation.removedOnCompletion = NO; [fadeOutAnimation setValue:layer forKey:@"parentLayer"]; [layer addAnimation:fadeOutAnimation forKey:@"opacity"]; [self.view.layer addSublayer:layer]; } } - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag { if(flag) { CALayer *layer = [theAnimation valueForKey:@"parentLayer"]; if(layer) { layer.opaque = NO; layer.opacity = 0.0; //layer.hidden = YES; //NSLog(@"The layer object was: %@ (%@)", layer, [layer name]); [layer removeFromSuperlayer]; [layer removeAllAnimations]; } } } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload