所以我有一些我想移動的NSButtons和NSImages(我想它們會以相同的方式移動)。我想將它們移動到窗口上的新座標上。我假設我可以找出我想在Interface Builder上移動它們的座標,然後以編程方式移動它們。如何在Mac上以編程方式移動界面對象?
此外,我該如何動畫這個動作?並控制動畫的速度,風格等等?是否有內置函數?還是必須製作圖像文件並循環播放?
注意:我主要想知道如何更改對象的座標,動畫是次要的,但如果您可以描述這一點,它會讓我很開心。
所以我有一些我想移動的NSButtons和NSImages(我想它們會以相同的方式移動)。我想將它們移動到窗口上的新座標上。我假設我可以找出我想在Interface Builder上移動它們的座標,然後以編程方式移動它們。如何在Mac上以編程方式移動界面對象?
此外,我該如何動畫這個動作?並控制動畫的速度,風格等等?是否有內置函數?還是必須製作圖像文件並循環播放?
注意:我主要想知道如何更改對象的座標,動畫是次要的,但如果您可以描述這一點,它會讓我很開心。
每個UI元素都有一個邊界和框架屬性。框架屬性具有x,y,寬度和高度屬性。如果要移動項目,只需調整框架屬性的值:
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];
更新:我原來的職位是有關如何在動畫2個rects一個iPhone,但這不是原來的海報/問題,這是我在24小時的編碼後試圖回答SO問題的原因,Anyhoo,我會在這裏留下這個答案,以防有人出現並想學習請注意,這個例子期望您首先在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舉例:
以下示範如何製作動畫2個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
問題很有趣,但請不要在您打算使用的應用程序中執行此操作。移動按鈕是一個用戶界面的噩夢。 – iwein 2010-10-29 10:53:55