gpt4 book ai didi

objective-c - 使用 nsbezierpath(objective -c)) 在 cocoa 中移动球/圆?

转载 作者:行者123 更新时间:2023-12-03 16:36:47 24 4
gpt4 key购买 nike

我试图在应用程序运行时在图像上创建一个点,然后尝试将该点缓慢移动到不同的位置。这是我的代码。该代码可以工作,但有两个问题。

首先,处理已经在窗口加载之前发生,因此我只能看到最终的结果。(我想显示一个点移动到图像中的另一个点)

第二,当我创建一个新点时,前一个点并没有被删除。所以它看起来不像是在移动,而是看起来像是在重复。如何删除一个点。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
NSGraphicsContext* gc = [NSGraphicsContext currentContext];

// Save the current graphics context settings
[gc saveGraphicsState];

// Set the color in the current graphics context for future draw operations
[[NSColor blackColor] setStroke];
[[NSColor redColor] setFill];


for(int i=1;i<100;i++){

NSRect rect = NSMakeRect(130+i, 130, 10, 10);
NSBezierPath* circlePath = [NSBezierPath bezierPath];
[circlePath appendBezierPathWithOvalInRect: rect];

// Outline and fill the path
[circlePath stroke];
[circlePath fill];


// // Restore the context to what it was before we messed with it
// [gc restoreGraphicsState];
}
}

最佳答案

编辑:

这是一个有效的实现:

@interface AppDelegate ()
@property ( nonatomic, readonly ) CALayer * ballLayer ;
@end

@implementation AppDelegate
@synthesize ballLayer = _ballLayer ;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[ ((NSView*)self.window.contentView) setWantsLayer:YES ] ;
[ self performSelectorOnMainThread:@selector( doAnimation ) withObject:nil waitUntilDone:NO ] ;
}

-(void)doAnimation
{
[ self.ballLayer addAnimation:[ self createBallLayerAnimation ] forKey:nil ] ;
}

-(CALayer*)ballLayer
{
if ( !_ballLayer )
{
CALayer * layer = [ CALayer layer ] ;
NSImage * image = [[ NSImage alloc ] initWithContentsOfURL:[ NSURL URLWithString:@"http://etc-mysitemyway.s3.amazonaws.com/icons/legacy-previews/icons/glossy-black-icons-sports-hobbies/044450-glossy-black-icon-sports-hobbies-ball-beach.png" ] ] ;
layer.contents = image ;
layer.bounds = (CGRect){ .size = { 100, 100 } } ;

[((NSView*)self.window.contentView).layer addSublayer:layer ] ;

_ballLayer = layer ;
}

return _ballLayer ;
}

-(CAAnimation*)createBallLayerAnimation
{
CAKeyframeAnimation * anim = [ CAKeyframeAnimation animationWithKeyPath:@"position" ] ;
{
CGPathRef p = [ self createBallAnimationPath ] ;
anim.path = p ;
CGPathRelease( p ) ;
}
anim.duration = 3.0 ;
anim.repeatCount = FLT_MAX ;

return anim ;
}

-(CGPathRef)createBallAnimationPath
{
CGRect bounds = ((NSView*)self.window.contentView).bounds ;
CGPathRef p = CGPathCreateWithEllipseInRect( CGRectInset( bounds, bounds.size.width * 0.25, bounds.size.width * 0.25 ), NULL ) ;
return p ;
}
@end
<小时/>

您需要阅读 CGPathCALayer...

正如其他人所说,不要在您的 applicationDidFinishLaunching 方法中执行此操作 - 您应该在窗口/ View 出现后执行此操作。如果您有自己的从 Nib 加载的 NSView 子类,一种选择可能是覆盖 -awakeFromNib:

-(void)awakeFromNib
{
[ super awakeFromNib ] ;
[ self performSelectorOnMainThread:@selector( doAnimation ) withObject:nil waitUntilDone:NO ] ; // when the main thread runs again, call `-doAnimation`
}

然后在您的 View 子类中还有一个 -doAnimation 方法(从上面的 -awakeFromNib 调用)

-(void)doAnimation:
{
CAAnimation * animation = [ CAKeyframeAnimation animationForKeyPath:@"position" ] ;
CGPathRef path = [ self createBallAnimationPath ] ; // method -createBallAnimationPath is defined below...
animation.path = path ;
CGPathRelease( path ) ;
[ self.ballLayer addAnimation:animation forKey:nil ] ; // ballLayer is the property that contains a reference to layer that contains the image you want to animate along the path
}

有一个方法来创建你的路径:

-(CGPathRef)createBallAnimationPath
{
CGMutablePathRef result = CGPathCreateMutable() ;
CGPathMoveToPoint( result, 100, 100 ) ;
CGPathAddLineToPoint( result, 1000, 1000 ) ;
return result ;
}

关于objective-c - 使用 nsbezierpath(objective -c)) 在 cocoa 中移动球/圆?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19650688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com