Effect:
After you overwrite the image attribute of uiimageview, you will be familiar with how uiimageview displays images:
Source code:
Uiimageview. H + uiimageview. m
//// Liveimageview. h // progress // copyright (c) 2014 l.s. all rights reserved. // # import <uikit/uikit. h> @ interface liveimageview: uiimageview @ property (nonatomic, assign) cgfloat duration; @ end
//// Liveimageview. M // progress // copyright (c) 2014 l.s. all rights reserved. // # import "liveimageview. H "@ interface liveimageview () {calayer * _ layer;} @ end @ implementation liveimageview-(ID) initwithframe :( cgrect) frame {self = [Super initwithframe: frame]; if (Self) {_ duration = 0.3f; _ layer = self. layer;} return self;} // rewrite the setter and getter methods of the image @ synthesize image = _ image;-(void) setimage :( uiimag E *) image {If (_ image! = Image) {cabasicanimation * ani = [cabasicanimation animationwithkeypath: @ "contents"]; ani. fromvalue = (_ bridge ID) (_ image. cgimage); ani. tovalue = (_ bridge ID) (image. cgimage); ani. duration = _ duration; _ layer. contents = (_ bridge ID) (image. cgimage); [_ layer addanimation: ANI forkey: Nil]; _ image = image ;}}- (uiimage *) image {return _ image ;}@ end
The following is the core code:
The setter method of the system is definitely written in this way. You can test it yourself. Because it is a backed layer, no explicit animation is assigned for the assignment.
// Lrootviewcontroller. M // uiimagetest // copyright (c) 2014 l.s. all rights reserved. // # import "lrootviewcontroller. H "# import" liveimageview. H "@ interface lrootviewcontroller () {nsarray * _ imagearray; liveimageview * liveview; _ block int count ;}@ end @ implementation lrootviewcontroller-(ID) initwithnibname :( nsstring *) nibnameornil bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {// custom initialization} return self;}-(void) viewdidload {[Super viewdidload]; _ imagearray = [nsarray loads: [uiimage imagenamed: @ "33.jpg"], [uiimage imagenamed: @" 44.jpg"], [uiimage imagenamed: @ "66.jpg"], nil]; liveview = [[liveimageview alloc] initwithframe: cgrectmake (0, 0,200,200)]; [self. view addsubview: liveview]; liveview. center = self. view. center; nstimer * timer = [nstimer scheduledtimerwithtimeinterval: 1.5 target: Self selector: @ selector (changeimage) userinfo: Nil repeats: Yes]; [Timer fire];}-(void) changeimage {// change the image [uiview animatewithduration: 0.3f animations: ^ {liveview. duration = 0.3f; liveview. image = _ imagearray [count ++ % 3]; // count ++ % 4 This is the object in the loop array // the effect of changing the size cgrect tmprect = liveview. bounds; tmprect. size = liveview. image. size; liveview. bounds = tmprect; liveview. center = self. view. center;}];}
The following is the animation code for changing the size.