Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK after another Google trawl: <a href="http://forums.macrumors.com/showthread.php?t=749518&amp;highlight=cabasicanimation" rel="nofollow noreferrer">http://forums.macrumors.com/showthread.php?t=749518&amp;highlight=cabasicanimation</a></p> <p>I found out about this thing called NSTimer! Another search of this site revealed some good info: <a href="https://stackoverflow.com/questions/1449035/how-do-i-use-nstimer">How do I use NSTimer?</a></p> <p>And about running NSTimer in its own thread: <a href="https://stackoverflow.com/questions/2318280/keep-an-nsthread-containing-an-nstimer-around-indefinitely-iphone">Keep an NSThread containing an NSTimer around indefinitely? (iPhone)</a></p> <p>So now I can offer this updated code example, which I hope may be useful to someone one day. By the way, I didn't try it yet, but apparently the following can be used to stop the timer:</p> <pre><code>[myTimer invalidate]; myTimer = nil; </code></pre> <p>Thanks to all here at this site, not only the most beautifully designed corner of the web but also a total lifesaver!</p> <pre><code> #import &lt;UIKit/UIKit.h&gt; #import &lt;QuartzCore/QuartzCore.h&gt; @interface Shape_05ViewController : UIViewController { CALayer *rootLayer; CAShapeLayer *shapeLayer; CGMutablePathRef pentagonPath; CGMutablePathRef starPath; int animationCount; CABasicAnimation *animation; NSTimer *repeatingTimer; } @property (assign, readwrite) NSTimer *repeatingTimer; -(void) startAnimation: (NSTimer*)theTimer; -(void) viewDidLoad; -(void) startSequence; @end </code></pre> <hr> <pre><code> #import "Shape_05ViewController.h" @implementation Shape_05ViewController @synthesize repeatingTimer; - (void)loadView { UIView *appView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; appView.backgroundColor = [UIColor blackColor]; self.view = appView; [appView release]; rootLayer = [CALayer layer]; rootLayer.frame = self.view.bounds; [self.view.layer addSublayer:rootLayer]; //Pentagon Path pentagonPath = CGPathCreateMutable(); CGPoint center = self.view.center; CGPathMoveToPoint(pentagonPath, nil, center.x, center.y - 75.0); for(int i = 1; i &lt; 5; ++i) { CGFloat x = - 75.0 * sinf(i * 2.0 * M_PI / 5.0); CGFloat y = - 75.0 * cosf(i * 2.0 * M_PI / 5.0); CGPathAddLineToPoint(pentagonPath, nil,center.x + x, center.y + y); } CGPathCloseSubpath(pentagonPath); //This seems to be fixed in 4.0 //Star Path starPath = CGPathCreateMutable(); center = self.view.center; CGPathMoveToPoint(starPath, NULL, center.x, center.y + 75.0); for(int i = 1; i &lt; 5; ++i) { CGFloat x = 75.0 * sinf(i * 4.0 * M_PI / 5.0); CGFloat y = 75.0 * cosf(i * 4.0 * M_PI / 5.0); CGPathAddLineToPoint(starPath, NULL, center.x + x, center.y + y); }; CGPathCloseSubpath(starPath); //Create Shape shapeLayer = [CAShapeLayer layer]; shapeLayer.path = pentagonPath; UIColor *fillColor = [UIColor colorWithWhite:0.9 alpha:1.0]; shapeLayer.fillColor = fillColor.CGColor; shapeLayer.fillRule = kCAFillRuleNonZero; [rootLayer addSublayer:shapeLayer]; } -(void) viewDidLoad { NSLog(@"viewDidLoad"); animationCount = 0; [NSThread detachNewThreadSelector:@selector(startSequence) toTarget:self withObject:nil]; } -(void) startSequence { NSLog(@"startSequence"); NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(startAnimation:) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] run]; self.repeatingTimer = timer; [pool release]; } -(void)startAnimation: (NSTimer*)theTimer { NSLog(@"startAnimation"); animation = [CABasicAnimation animationWithKeyPath:@"path"]; animation.duration = 0.25; animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; animation.repeatCount = 0; animation.autoreverses = NO; animation.fillMode = kCAFillModeForwards; animation.removedOnCompletion = NO; if( (animationCount % 2) == 0) { animation.fromValue = (id)pentagonPath; animation.toValue = (id)starPath; } else { animation.fromValue = (id)starPath; animation.toValue = (id)pentagonPath; }; [shapeLayer addAnimation:animation forKey:@"animatePath"]; animationCount++; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)dealloc { CGPathRelease(starPath); CGPathRelease(pentagonPath); [super dealloc]; } @end </code></pre>
    singulars
    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.
    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