【发布时间】:2012-02-16 17:46:36
【问题描述】:
我有一个 UIImageView,它有一个方法来改变它显示的图像。如果该方法是从 UIImageView 本身接收到的触摸事件触发的,则它可以正常工作,但是如果从另一个触发触摸事件的对象调用相同的方法,则无法更新 UIImageView。我在有问题的方法中有一个 NSLog 调用,因此可以看到,在这两种情况下都调用了该方法。但只有在一种情况下,我才能看到图像的实际变化,而在另一种情况下,视图没有更新。 当它工作时,我不需要 setNeedsDisplay 或 setNeedsLayout,但这就是我试图解决问题的方法。在 UIImageView 及其父视图中设置 needsDisplay 和 needsLayout。无济于事。我可以看到,如果我旋转设备会导致刷新并且我看到 UIImageView 确实发生了变化,那么图像实际上已经改变了。
superview 使用 makeObjectsPerformSelector:withObject: 在 OutletCollection 上调用方法 eventAtLocation:
在我看来,这确实是一个令人尴尬的错误,但几个小时以来我都想不通。我没有想法可以尝试什么:-(
这是有问题的代码:
- (void)eventAtLocation:(NSValue *)location
{
CGPoint loc = [self.superview convertPoint:[location CGPointValue] toView:self];
if ([self pointInside:loc withEvent:nil]) {
if (!self.isAnimating) {
[self autoSwapImage];
}
}
else{
if (self.isAnimating) {
[self cancelAnimation];
}
}
}
- (void)cancelAnimation
{
self.isAnimating = NO;
[NSObject cancelPreviousPerformRequestsWithTarget:self];
}
- (void) swapImage
{
currentImage++;
if (currentImage > numberOfImages)
currentImage = 1;
NSLog(@"set image to %i", currentImage);
self.image = [UIImage imageNamed:[NSString stringWithFormat:@"img_%i.jpg", currentImage]];
[self setNeedsDisplay];
}
- (void) autoSwapImage
{
self.isAnimating = YES;
[self swapImage];
[self performSelector:@selector(autoSwapImage) withObject:self afterDelay:0.1];
}
// 下面的工作,但如果 eventAtLocation: 被称为 superview swapImage 被调用(-> NSLog 输出出现),但视图没有更新 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //[自autoSwapImage]; }
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//[NSObject cancelPreviousPerformRequestsWithTarget:self];
}
@end
【问题讨论】:
标签: objective-c ios cocoa-touch