cocoa - Mac OSX - Core Animation - how to animate something which is following my cursor? -
so far have seen core animation has code looks this, parameters of animate set in beginning.
- (void) starttopleftimageviewanimation{ /* start top left corner */ [self.xcodeimageview1 setframe:cgrectmake(0.0f,0.0f, 100.0f, 100.0f)]; [self.xcodeimageview1 setalpha:1.0f]; [uiview beginanimations:@"xcodeimageview1animation" context:(__bridge void *)self.xcodeimageview1]; /* 3 seconds animation */ [uiview setanimationduration:3.0f]; /* receive animation delegates */ [uiview setanimationdelegate:self]; [uiview setanimationdidstopselector: @selector(imageviewdidstop:finished:context:)]; /* end @ bottom right corner */ [self.xcodeimageview1 setframe:cgrectmake(220.0f, 350.0f, 100.0f, [self.xcodeimageview1 setalpha:0.0f]; [uiview commitanimations]; }
however, make animation in graphic follows mouse cursor. means animation receiving mouse-pointer x-y values.
is there better alternative doing "manual" animation this, , calling drawrect whenever shift position of mouse? can use core animation this?
- (void)drawrect:(nsrect)rect { // drawing code here. [[nscolor whitecolor] set]; nsrectfill( rect ); [self drawcircleatpoint:_circlept]; } - (void) drawcircleatpoint:(cgpoint) point { nsbezierpath* thepath = [nsbezierpath bezierpath]; nsrect nrect = nsmakerect(point.x, point.y, 50, 50); [thepath appendbezierpathwithovalinrect:nrect]; [[nscolor blackcolor] set]; [thepath stroke]; }
thanks regarding this.
first need point mouse cursor pointing to. method have implement is
- (void)mousemoved:(nsevent *)theevent
nsevent
has method called
- (nspoint)locationinwindow
which gives you, might guessed, position of cursor in window.
now have find location within view. done nsview
method
- (nspoint)convertpoint:(nspoint)apoint fromview:(nsview *)aview
where point comes locationinwindow
, view view in want perform animation
e.g.
cgpoint mousepoint = [[[self window] contentview] convertpoint:[theevent locationinwindow] fromview:self];
then can either set position via views frame or can use position property of views layer.
Comments
Post a Comment