Override the image attribute of uiimageview ---

Source: Internet
Author: User

 

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.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.