Objective c 如何在Mac上以编程方式移动接口对象?
所以我有一些NSButton和NSImage(我想它们会以相同的方式移动),我想移动它们。我想把它们移到窗口上的新坐标。我假设我可以在Interface Builder上计算出要将它们移动到的坐标,然后以编程方式移动它们 此外,如何设置此运动的动画?控制动画的速度、风格等等?有内置的功能吗?还是我必须制作图像文件并循环浏览Objective c 如何在Mac上以编程方式移动接口对象?,objective-c,Objective C,所以我有一些NSButton和NSImage(我想它们会以相同的方式移动),我想移动它们。我想把它们移到窗口上的新坐标。我假设我可以在Interface Builder上计算出要将它们移动到的坐标,然后以编程方式移动它们 此外,如何设置此运动的动画?控制动画的速度、风格等等?有内置的功能吗?还是我必须制作图像文件并循环浏览 注意:我主要想知道如何更改对象的坐标,动画是次要的,但如果您也能描述一下,我会非常高兴。每个UI元素都有一个边界和帧属性。frame属性具有x、y、width和height属
注意:我主要想知道如何更改对象的坐标,动画是次要的,但如果您也能描述一下,我会非常高兴。每个UI元素都有一个边界和帧属性。frame属性具有x、y、width和height属性。如果要四处移动项目,只需调整框架特性的值:
NSButton *button // assume this exists as your UI object
NSRect rect = NSMakeRect(50.0, 40.0, 250.0, 100.0);
[button setFrame:rect];
这将在没有任何动画的情况下移动它。如果需要动画,请通过对象的动画代理(适用于简单内容:
[[button animation] setFrame:rect];
你可以使用核心动画来实现这一点:更新:我最初的帖子是关于如何在iPhone上制作两个矩形的动画,但这不是最初的海报/问题所问的。这就是我在24小时的编码后试图回答SO问题所得到的结果。不管怎样,我将把这个答案留在这里,以防有人来学习关于iPhone上的这一点。下面是一个在Mac上执行完全相同操作的示例。请注意,此示例要求您首先在Interface Builder中使用NSWindow设置一个示例项目(任何默认项目都应该这样做): ------Mac示例:
// MacAnimationExample.h:
#import <Cocoa/Cocoa.h>
#import <QuartzCore/QuartzCore.h>
@interface ColorView : NSView {
NSColor *backgroundColor;
}
@property( nonatomic, retain ) NSColor *backgroundColor;
@end
@interface MacAnimationExample : NSObject <NSAnimationDelegate, NSApplicationDelegate> {
ColorView *red;
ColorView *yellow;
IBOutlet NSWindow *window;
}
@property( assign ) IBOutlet NSWindow *window;
@end
// MacAnimationExample.m:
#import "MacAnimationExample.h"
@implementation ColorView
@synthesize backgroundColor;
- (void) setBackgroundColor:(NSColor *) iColor {
[iColor retain];
[backgroundColor release];
backgroundColor = iColor;
[self setNeedsDisplay: YES];
}
- (void) drawRect:(NSRect) iRect {
[backgroundColor set];
NSRectFill( iRect );
}
@end
@implementation MacAnimationExample
@synthesize window;
- (void) animateIt {
NSAnimation *animation;
NSArray *animations;
NSRect redFrame, yellowFrame;
NSMutableDictionary *redInfo, *yellowInfo;
redFrame = [red frame];
yellowFrame = [yellow frame];
redInfo = [NSMutableDictionary dictionary];
[redInfo setObject: red forKey: NSViewAnimationTargetKey];
[redInfo setObject: [NSValue valueWithRect: redFrame] forKey: NSViewAnimationStartFrameKey];
[redInfo setObject: [NSValue valueWithRect: yellowFrame] forKey: NSViewAnimationEndFrameKey];
yellowInfo = [NSMutableDictionary dictionary];
[yellowInfo setObject: yellow forKey: NSViewAnimationTargetKey];
[yellowInfo setObject: [NSValue valueWithRect: yellowFrame] forKey: NSViewAnimationStartFrameKey];
[yellowInfo setObject: [NSValue valueWithRect: redFrame] forKey: NSViewAnimationEndFrameKey];
animations = [NSArray arrayWithObjects: redInfo, yellowInfo, nil];
animation = [[[NSViewAnimation alloc] initWithViewAnimations: animations] autorelease];
[animation setDelegate: self];
[animation setDuration: 0.5];
[animation startAnimation];
}
- (void) animationDidEnd:(NSAnimation*) animation {
[self animateIt];
}
- (void) applicationDidFinishLaunching:(NSNotification *) aNotification {
NSRect bounds;
NSView *contentView;
contentView = [window contentView];
bounds = [contentView bounds];
bounds.size.width = floorf( bounds.size.width * 0.5 );
red = [[[ColorView alloc] initWithFrame: bounds] autorelease];
red.backgroundColor = [NSColor redColor];
[contentView addSubview: red];
bounds.origin.x += bounds.size.width;
yellow = [[[ColorView alloc] initWithFrame: bounds] autorelease];
yellow.backgroundColor = [NSColor yellowColor];
[contentView addSubview: yellow];
[self animateIt];
}
@end
我假设动画不是字面上所使用的方法,你有一个具体的例子吗?哦,好吧,这是animator,不是animation,太棒了。对不起,是的,我在没有实际编译的情况下键入了这个。想法是,对于简单的动画,你不需要直接处理核心动画。相反,你可以询问NSView(或其子类)对于动画代理。代理会传递您想要发送的消息,但它允许您对其进行动画处理,而不仅仅是移动。谢谢,这篇文章很棒。问题是关于mac开发的。不是iPhone。嗯,是的。我的NS和UI看起来很混乱。我会去看看如何为mac编写同样的内容。问题很有趣g、 但请不要在你打算让我使用的应用程序中这样做。移动按钮是UI的噩梦。
@interface ExampleViewController : UIViewController {
UIView *red;
UIView *yellow;
}
@end
@implementation ExampleViewController
- (void) loadView {
UIView *background;
CGRect frame;
frame = [UIScreen mainScreen].bounds;
// background view contains red & yellow subviews
background = [[[UIView alloc] initWithFrame: frame] autorelease];
frame.size.width = floorf( frame.size.width * 0.5 );
red = [[[UIView alloc] initWithFrame: frame] autorelease];
red.backgroundColor = [UIColor redColor];
frame.origin.x = frame.size.width;
yellow = [[[UIView alloc] initWithFrame: frame] autorelease];
yellow.backgroundColor = [UIColor yellowColor];
[background addSubview: yellow];
[background addSubview: red];
self.view = background;
}
- (void) viewWillAppear:(BOOL) animated {
CGRect temp;
// normally you will let the animated parameter
// control whether or not the view animates. for
// demonstation I add a hack to force animation to begin:
animated = YES; // hack
if ( animated ) {
[UIView beginAnimations: nil context: nil];
[UIView setAnimationDelegate: self];
[UIView setAnimationDidStopSelector: @selector(startAnimation)];
[UIView setAnimationDuration: 0.5];
}
temp = red.frame;
red.frame = yellow.frame;
yellow.frame = temp;
if ( animated ) [UIView commitAnimations];
}
- (void) startAnimation {
[self viewWillAppear: YES];
}
@end