如何在Mac上以编程方式移动界面对象?

时间:2010-10-20 00:42:16

标签: objective-c

所以我有一些我想移动的NSButtons和NSImages(我认为它们会以同样的方式移动)。我想将它们移动到窗口上的新坐标。我假设我可以找到我想在Interface Builder上移动它们的坐标,然后以编程方式移动它们。

此外,我该如何设置此动作的动画?并控制动画的速度,风格等等?有内置功能吗?或者我是否必须制作图像文件并循环浏览它们?

注意:我主要想知道如何更改对象的坐标,动画是次要的,但是如果你能描述它也会让我很开心。

3 个答案:

答案 0 :(得分:5)

每个UI元素都有一个bounds和frame属性。 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];

答案 1 :(得分:4)

更新:我的原始帖子是关于如何在iPhone上制作两个动画,但这不是原始海报/问题所要求的。这就是我在24小时编码后尝试回答SO问题所得到的。 Anyhoo,我将在这里留下答案,以防有人出现并希望在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

------ iPhone示例:

这是一个示例,说明如何动画两个rects。一旦你搞清楚这个简单的基于UIView的动画的其余部分是非常容易的。您可以查看较低层的CoreAnimation内容,而对于较新的操作系统,您可以查看动画块概念。这是你应该开始的地方:

@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

答案 2 :(得分:3)

您可以使用核心动画:http://www.cocoadev.com/index.pl?CoreAnimation