Objective c 如何设置UIButton的图像并随后将其删除(动画)
我试图将UIButton的图像设置为某个图像一段时间,然后在这段时间之后,我希望按钮删除该图像。这是我一直在使用的代码,它无法正常工作。如果有人能帮忙,我将不胜感激 谢谢Objective c 如何设置UIButton的图像并随后将其删除(动画),objective-c,animation,Objective C,Animation,我试图将UIButton的图像设置为某个图像一段时间,然后在这段时间之后,我希望按钮删除该图像。这是我一直在使用的代码,它无法正常工作。如果有人能帮忙,我将不胜感激 谢谢 [UIView animateWithDuration:5.0 animations:^{ [button setImage:[UIImage imageNamed:@"light.png"] forState:UIControlStateNormal]; }completion:^(BOOL finisheds){
[UIView animateWithDuration:5.0 animations:^{
[button setImage:[UIImage imageNamed:@"light.png"] forState:UIControlStateNormal];
}completion:^(BOOL finisheds){
[button setImage:nil forState:UIControlStateNormal;
}];
如果在类中定义一个方法,如下所示:
- (void)removeImageFromButton:(id)button {
[button setImage:nil forState:UIControlStateNormal]
}
[button setImage:[UIImage imageNamed:@"light.png"] forState:UIControlStateNormal];
[self performSelector:@selector(removeImageFromButton:) withObject:button afterDelay:5.0];
然后你可以像这样做你想做的事情:
- (void)removeImageFromButton:(id)button {
[button setImage:nil forState:UIControlStateNormal]
}
[button setImage:[UIImage imageNamed:@"light.png"] forState:UIControlStateNormal];
[self performSelector:@selector(removeImageFromButton:) withObject:button afterDelay:5.0];
另外,您可能需要添加[NSObject cancelpreviousperformselector requestswithtarget:self]代码>在-(void)dealloc
中,或者如果self
在5s启动前被解除分配,您将获得EXC\u BAD\u访问权限