【问题标题】:Differents behavior with setter/getter in subclass of UIImageView with iOS 7.1iOS 7.1 的 UIImageView 子类中的 setter/getter 的不同行为
【发布时间】:2014-04-19 10:36:41
【问题描述】:

从 iOS 7.1 beta 5 开始,我的小插件出现了一个奇怪的错误:link

我的插件是UIImageView 的子类,在这个类里面我有另一个UIImageView 叫做img

我已经重写了image的setter和getter:

- (void)setImage:(UIImage *)image{

    self.img.image = image;

}

- (UIImage *)image{

    return _img.image;

}

所以NSLog(@"%@",self.image) 返回_img.image,我没事。

NSLog(@"%@",[super image])返回(null),我没事。

问题在于 iOS 7.0 self 不显示图像,但 iOS 7.1 self 显示 _img.image

在视觉上,我不想要 self 的图像,我想要与 iOS 7.0 相同的行为。

所以我尝试另一件事来理解,如果我使用这个 setter/getter :

- (void)setImage:(UIImage *)image{

    [super setImage:[UIImage imageNamed:@"becomeapanda_tumblr_com_portrait"]];

}

- (UIImage *)image{

    return [UIImage imageNamed:@"becomeapanda_tumblr_com"];

}

使用 iOS 7.0,self 显示此图像“becomeapanda_tumblr_com_portrait”,但使用 iOS 7.1 self 显示此图像“becomeapanda_tumblr_com”。

如果我能帮助我理解为什么我的插件在 iOS 7.1 中没有相同的行为,那就太好了!

【问题讨论】:

  • 在我看来,内部实现是直接在 iOS 7.0 中引用图像 ivar,而现在在 iOS7.1 中它使用的是属性 getter。
  • 是的,我同意你,但这是一个巨大的修改。我已经阅读了 iOS 7.1 Apple 的文档,但对此一无所知。
  • Apple 没有记录他们的内部实现。
  • 您可能需要执行this 之类的操作来查看谁在调用getter,如果是self 调用,则返回nil

标签: ios objective-c uiimageview ios7.1


【解决方案1】:

您好,试试看,您忘记将 [self animaticToScaleAspectFit] 放入您的方法中,

    - (void)animateToScaleAspectFitToFrame:(CGRect)frame WithDuration:(float)duration afterDelay:(float)delay{

    if (![self uiimageIsEmpty]) {
        [self initToScaleAspectFitToFrame:frame];

        [UIView animateWithDuration:duration delay:delay options:UIViewAnimationOptionAllowUserInteraction
                         animations:^{
                             [self animaticToScaleAspectFit];
                         } completion:nil];
    }else{
        [self animaticToScaleAspectFit];
        NSLog(@"ERROR, UIImageView %@ don't have UIImage",self);
    }  
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多