Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding UIImageView's image getter/setter methods

I'm subclassing a UIImageView, so that each time the image property is set an animation occurs. The following was successful:

import UIKit

class AnimatedImageView: UIImageView {

var img: UIImage! {
    get {
        return self.image
    }
    set {
        self.image = newValue

        UIView.animateWithDuration(0.5, delay: 0.4, usingSpringWithDamping: 0.2, initialSpringVelocity: 5.0, options: .CurveEaseIn, animations: {_ in

            self.transform = CGAffineTransformMakeScale(1.1, 1.1);

            }, completion: {_ in
                self.transform = CGAffineTransformIdentity;
        })
    }
}

This is no surprise. I subclassed UIImageView and added an entirely new variable called 'img', which in turn modifies UIImageView's 'image' property.

The issue is that the end-user could conceivably still alter AnimatedImageView's 'image' property.

import UIKit

class AnimatedImageView: UIImageView {

override var image: UIImage! {
 get {
     return self.image
 }
 set {
    self.image = newValue

    UIView.animateWithDuration(0.5, delay: 0.4, usingSpringWithDamping: 0.2, initialSpringVelocity: 5.0, options: .CurveEaseIn, animations: {_ in

        self.transform = CGAffineTransformMakeScale(1.1, 1.1);

        }, completion: {_ in
            self.transform = CGAffineTransformIdentity;
    })
 }
}

Sure enough this causes a stackoverflow because when I call self.image = newValue it repeatedly calls the setter method that I've overridden in my subclass. So, what's the right way to override the getter/setter methods of the 'image' property on UIImageView

like image 942
cph2117 Avatar asked Dec 02 '15 22:12

cph2117


1 Answers

Just use super.image instead, that will prevent a loop.

like image 154
TheEye Avatar answered Nov 01 '22 10:11

TheEye