ios - UIButton 在 setImage : is called 后不更新它的图像

标签 ios uiview uibutton

这就是问题所在 - 我有一个类,它是 UIView(一张卡片)的子类。它在自身上创建一个按钮并调用 changeStateIndicator: 当用户触摸它时。

然后卡片必须在按下按钮后翻转并且按钮应该改变它的颜色(实际上是图像)。但它根本没有发生,所以翻转在按钮改变之前开始。这是我的代码:

//Adding a button to my view
UIButton *changeStateButton = [UIButton buttonWithType:UIButtonTypeCustom];
[changeStateButton setImage:[UIImage imageNamed:imageToInitWith] forState:UIControlStateNormal];
[changeStateButton setImage:[UIImage imageNamed:imageToInitWith] forState:UIControlStateHighlighted];
changeStateButton.frame = CGRectMake(0, 0, 30, 30);
changeStateButton.center = CGPointMake(self.bounds.size.width/2+([myWord length]*8)+17, 35);
changeStateButton.tag = 77;
[changeStateButton addTarget:self action:@selector(changeStateIndicator:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:changeStateButton];

//Method which is called when the button is touched
- (void)changeStateIndicator:(UIButton *)sender
{
    [sender setImage:[UIImage imageNamed:@"StateYellow"] forState:UIControlStateNormal];
    [sender setImage:[UIImage imageNamed:@"StateYellow"] forState:UIControlStateHighlighted];
    currentWord.done = [NSNumber numberWithInt:currentWord.done.intValue-10];
    [self prepareForTest];
}

//Method which flips the card
- (void)prepareForTest
{
    testSide = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cal_small3"]];
    [UIView transitionFromView:self toView:testSide duration:0.5 options:UIViewAnimationOptionTransitionFlipFromRight completion:nil];
}

我的猜测是发件人在方法 -changeStateIndicator 结束之前不会更改其图像,但我不太确定这是实际问题。请帮助我。

最佳答案

尝试推迟这个调用:

dispatch_async(dispatch_get_main_queue(), ^{ [self prepareForTest]; });

如果这种方法有效但仍然不是您想要的,那么使用 dispatch_after() 或

[self performSelector:@selector(prepareForTest) withObject:nil afterDelay:0.25f];

(如果需要,您可以使用时间为“0”的第二次调用而不是调度 - 同样的事情。)

关于ios - UIButton 在 setImage : is called 后不更新它的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11639551/

相关文章:

表格 View 上的 iOS float 按钮

ios - 动画在某些 View Controller 中不起作用

iphone - UIViewController 子类支持 NIB 加载和非

ios - 我可以在任意 UIView 变量上设置动画吗?

ios - UITableViewCell 中的 UIButton 标题在重用时发生变化

ios - MonoTouch 自定义 UIButton 在加载时崩溃 : Selector invoked from objective-c on a managed object that has been GC'd

ios - 在 iOS 中传递上下文以将 Core Data 与 Storyboard 一起使用

ios - 删除使用 NSMutableAttributedString 的 UILabel 空白区域的彩色背景

iOS - UIView setCenter 在 UIView animateWithDuration 中不起作用

ios - 如何设置UIButton的标题文字颜色?