![](/img/trans.png)
[英]IOS/Objective-C: Assign custom image to UIBarButtonItem
[英]IOS/Objective-C: Animate Image Change in UIBarButtonItem
我正在嘗試創建一種效果,其中UIBarButtonItem中的自定義圖像會更改為另一個圖像。 到目前為止,我已經可以使用以下代碼來分解第一個圖像。 誰能建議我如何讓第二張圖片同時淡入淡出?
//Create barbuttonitem in viewwillappear
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setFrame:CGRectMake(12, 0, 22, 22)];
[b addTarget:self action:@selector(menuButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed:@"firstimage.png"] forState:UIControlStateNormal];
UIBarButtonItem * myBarButton = [[UIBarButtonItem alloc] initWithCustomView:b];
//Animate in viewDidAppaer
[UIView animateWithDuration:1.0
delay:1.0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.myBarButton.customView.alpha = 0;
}
completion:^(BOOL finished) {
UIImage *secondImage = [UIImage imageNamed:@"menu2.png"];
[self.myBarButton setImage:secondImage];//THis does not change image
self.myBarButton.customView.alpha = 1;//no animation
}];
你可以用這樣的東西
[UIView transitionWithView:self.myBarButton
duration:0.3
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{ [self.myBarButton setImage:secondImage];}
completion:nil];
Try this code
@interface ViewController ()
{
UIBarButtonItem * myBarButton;
NSTimer *time;
UIButton *b;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setFrame:CGRectMake(12, 0, 22, 22)];
[b addTarget:self action:@selector(menuButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[b setImage:[UIImage imageNamed:@"AddPlus_Yellow.png"] forState:UIControlStateNormal];
myBarButton = [[UIBarButtonItem alloc] initWithCustomView:b];
self.navigationItem.rightBarButtonItem=myBarButton;
time = [NSTimer scheduledTimerWithTimeInterval: 2.0 target: self selector:@selector(onTick:) userInfo: nil repeats:NO];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)onTick:(NSTimer *)timer {
[b setImage:[UIImage imageNamed:@"filter.png"] forState:UIControlStateNormal];
[time invalidate];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.