問題描述
如何設置 UIButton 的圖像並隨後將其刪除(動畫) (How to setImage of a UIButton and subsequently remove it (animation))
I'm trying to set the image of a UIButton to some image for a set amount of time, and then after that time, I want the button to remove the image. Here is the code I have been using and it isn't seaming to work. If anyone can help, I'd greatly appreciate it!
Thanks!
[UIView animateWithDuration:5.0 animations:^{
[button setImage:[UIImage imageNamed:@"light.png"] forState:UIControlStateNormal];
}completion:^(BOOL finisheds){
[button setImage:nil forState:UIControlStateNormal;
}];
‑‑‑‑‑
參考解法
方法 1:
If you define a method in your class, something like this:
‑ (void)removeImageFromButton:(id)button {
[button setImage:nil forState:UIControlStateNormal]
}
Then you can do what you want to do like this:
[button setImage:[UIImage imageNamed:@"light.png"] forState:UIControlStateNormal];
[self performSelector:@selector(removeImageFromButton:) withObject:button afterDelay:5.0];
Also, you probably want to add [NSObject cancelPreviousPerformSelectorRequestsWithTarget:self];
in ‑ (void)dealloc
or else you make get a EXC_BAD_ACCESS
if self
is deallocated before the 5s is up.
(by Josh Sklar、Darren Clark)