2011-07-07 33 views
16

Tôi muốn đặt một biểu tượng trong thanh trạng thái Mac OS như một phần của ứng dụng ca cao của mình. Những gì tôi làm ngay bây giờ là:Cách thêm biểu tượng động vào thanh trạng thái OS X?

NSStatusBar *bar = [NSStatusBar systemStatusBar]; 

sbItem = [bar statusItemWithLength:NSVariableStatusItemLength]; 
[sbItem retain]; 

[sbItem setImage:[NSImage imageNamed:@"Taski_bar_icon.png"]]; 
[sbItem setHighlightMode:YES]; 
[sbItem setAction:@selector(stopStart)]; 

nhưng nếu tôi muốn biểu tượng hoạt ảnh (3-4 khung), tôi làm cách nào?

+2

Vâng, tôi muốn tạo ấn tượng rằng ứng dụng đang xử lý dữ liệu - hiếm khi xảy ra nhưng có thể là điều cần biết. Hay tôi không nên? – kender

+8

Đó là sử dụng hợp lệ. Heck, Time Machine làm được. –

+4

Tôi nghĩ Dropbox xử lý điều này khá tốt. Nó tinh tế nhưng nó nói với bạn rằng mọi thứ đang được cập nhật. –

Trả lời

27

Bạn cần phải liên tục gọi -setImage: trên số NSStatusItem của mình, chuyển từng hình ảnh khác nhau mỗi lần. Cách dễ nhất để thực hiện điều này là với NSTimer và một biến mẫu để lưu trữ khung hiện tại của hoạt ảnh.

Something như thế này:

/* 

assume these instance variables are defined: 

NSInteger currentFrame; 
NSTimer* animTimer; 

*/ 

- (void)startAnimating 
{ 
    currentFrame = 0; 
    animTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/30.0 target:self selector:@selector(updateImage:) userInfo:nil repeats:YES]; 
} 

- (void)stopAnimating 
{ 
    [animTimer invalidate]; 
} 

- (void)updateImage:(NSTimer*)timer 
{ 
    //get the image for the current frame 
    NSImage* image = [NSImage imageNamed:[NSString stringWithFormat:@"image%d",currentFrame]]; 
    [statusBarItem setImage:image]; 
    currentFrame++; 
    if (currentFrame % 4 == 0) { 
     currentFrame = 0; 
    } 
} 
+0

Có thể đặt NSView hoặc NSImageView vào một mục trên thanh trạng thái không? Làm thế nào để làm nó? – zsong

+0

bạn có phải yêu cầu animTimer kích hoạt qua [fireTimer fire] hay tự động không? –

+1

Các phương thức '+ scheduledTimerWithTimeInterval: ...' sẽ thêm bộ đếm thời gian vào vòng lặp chạy cho bạn (đó là bit 'đã lập lịch biểu'), do đó bạn không cần phải tự hẹn giờ. –

5

Tôi đã viết lại giải pháp của Rob vì vậy mà tôi có thể tái sử dụng nó:

Tôi có số khung hình 9 và tất cả các tên hình ảnh có chữ số cuối cùng là số khung hình cho tôi có thể đặt lại hình ảnh mỗi lần để tạo hiệu ứng biểu tượng.

//IntervalAnimator.h 

    #import <Foundation/Foundation.h> 

@protocol IntervalAnimatorDelegate <NSObject> 

- (void)onUpdate; 

@end 


@interface IntervalAnimator : NSObject 
{ 
    NSInteger numberOfFrames; 
    NSInteger currentFrame; 
    __unsafe_unretained id <IntervalAnimatorDelegate> delegate; 
} 

@property(assign) id <IntervalAnimatorDelegate> delegate; 
@property (nonatomic) NSInteger numberOfFrames; 
@property (nonatomic) NSInteger currentFrame; 

- (void)startAnimating; 
- (void)stopAnimating; 
@end 

#import "IntervalAnimator.h" 

@interface IntervalAnimator() 
{ 
    NSTimer* animTimer; 
} 

@end 

@implementation IntervalAnimator 
@synthesize numberOfFrames; 
@synthesize delegate; 
@synthesize currentFrame; 

- (void)startAnimating 
{ 
    currentFrame = -1; 
    animTimer = [NSTimer scheduledTimerWithTimeInterval:0.50 target:delegate selector:@selector(onUpdate) userInfo:nil repeats:YES]; 
} 

- (void)stopAnimating 
{ 
    [animTimer invalidate]; 
} 

@end 

Làm thế nào để sử dụng:

  1. Phù hợp với giao thức trong lớp StatusMenu bạn

    //create IntervalAnimator object 
    
    animator = [[IntervalAnimator alloc] init]; 
    
    [animator setDelegate:self]; 
    
    [animator setNumberOfFrames:9]; 
    
    [animator startAnimating]; 
    
  2. Thực hiện phương pháp giao thức

    -(void)onUpdate { 
    
        [animator setCurrentFrame:([animator currentFrame] + 1)%[animator numberOfFrames]]; 
    
        NSImage* image = [NSImage imageNamed:[NSString stringWithFormat:@"icon%ld", (long)[animator currentFrame]]]; 
    
        [statusItem setImage:image]; 
    
    } 
    
Các vấn đề liên quan