An introduction to the "iOS Dev-9" Picture property, which implements an animated effect similar to "click-to-Focus", and the way to implement a start/stop button toggle function

Source: Internet
Author: User

(1) If the button triggers an event method, we only need to pass in the control object by sender (ID), this method can handle the control property value, and if the method needs to handle other objects, then one method is to set the object as a global variable so that all methods can use the object. and set its properties, our imgView2 here is this way, you can control whether it stops or starts in the method.


(2) UIImage and Uiimageview are usually a pair, UIImage object just add the picture to the program, but it is not the view cannot be loaded display in the app, but Uiimageview is a view, You can initialize the UIImage object to the Uiimageview object, and then let the Uiimageview object be loaded into the view to display it. From the last we used [Self.view addsubview:imgview1] to be visible, we're going to load a child view, but the child view is a view.


(3) The picture content mode is more important, that is, how to put the picture in the frame of the Uiimageview, is the play, on the left or equal scale to fill, or unequal proportions as long as the fill can.


(4) To achieve the animation effect, in fact, is to get a series of pictures and then give Uiimageview object animation picture properties, as shown in this example is usually the picture in the array, and then passed; Set playback time and frequency after delivery, etc., Finally use startanimating and stopanimating and other methods to control whether to play, of course, remember to load this view in order to display.


(5) A review of the UIButton method, that is, how to achieve the "point pause as a player, the point of play will become a Pause" button style.

* found in iOS7 These new versions of the Tintcolor property (set button background when clicked ) is invalid, so in order to click on a bit of feeling, you can change the background color method instead.


#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller{//In order to invoke this object in other methods, It needs to be set as a global variable, not just Uiimageview *imgview2 in Viewdidload;} -(void) Viewdidload {//uiimage instantiates a Img1 object and initializes a picture to it, which is equivalent to moving the picture into the application UIImage *img1=[uiimage imagenamed:@ "logo.pn    G "]; Then uiimageview equivalent to a vector, used to display the Picture object//Picture itself can not be displayed in the program, need a carrier, such as before the UIButton, the picture is the use of buttons this carrier display// So UIImage object IMG1 no frame frame//Only Uiimageview object ImgView1 has frame property, it is loaded with pictures, and then we set its position size to Uiimageview *imgview1=[[    Uiimageview ALLOC]INITWITHIMAGE:IMG1];    Unsurprisingly: The image is automatically scaled and scaled to frame size imgview1.frame=cgrectmake (30, 30, 300, 80);    What if we need a little original?        (1) If you know the original width height, write the numbers directly//(2) Call the image size in the width of the high property, such as Imgview1.frame=cgrectmake (, Img1.size.width, Img1.size.height)    With this, if I want to keep the width of the imgView1, and want the picture not to be scaled//need to use the Content mode Properties//First get a background to do the effect demonstration with Imgview1.backgroundcolor=[uicolor Redcolor]; Try one, find the center, and the picture is not scaled//This contentmode content mode is UIView method, not just UIImage (inherit from UIView) method, so we look at its type in front of the UIView, such as Uiviewcontentmodecenter//[ImgView1 Setcontentmode:uiviewcontentmodecenter]; Other types are fixed positions, such as upper and lower left and right, the following is the focus and common//uiviewcontentmodescaletofill is filled, of course, the picture will be unequal scaling, and no content mode properties before// The uiviewcontentmodescaleaspectfill is filled with proportional scaling, and it can be imagined that one side is exactly the same length, and that one side may be beyond the frame boundary of the entire imgView1. Uiviewcontentmodescaleaspectfit is a proportional-scaled fill to the maximum size, you can imagine that one side of the length is just right, and one side may not yet reach the frame's boundary [ImgView1 Setcontentmode:    Uiviewcontentmodecenter];        [Self.view Addsubview:imgview1];    Set animation//Get a Uiimageview ready to load, originally Uiimageview *imgview2=[[uiimageview Alloc]init] But ImgView2 is declared as a global variable at the top, just initialize it here    Imgview2=[[uiimageview Alloc]init];    Imgview2.frame=cgrectmake (30, 150, 40, 40);    Import a series of images that need to be animated into an array, without using a For loop to import Nsmutablearray *arr1=[[nsmutablearray alloc]init];        for (int i=1; i<=3; i++) {UIImage *img2=[uiimage imagenamed:[nsstring stringwithformat:@ "%i.png", I]];    [Arr1 Addobject:img2];    }//1, the first to assemble this number into the animated picture imgview2.animationimages=arr1; 2, set the playback time, that is, each time full play Imgview2.animationduraTion=1;    3, set the repeat number of times, such as not set or 0 o'clock, indicating an infinite loop imgview2.animationrepeatcount=0;    4, also to start, in order to play [ImgView2 startanimating];        Of course, it must be loaded in order to display [Self.view ADDSUBVIEW:IMGVIEW2];    We add a button to achieve start and stop function UIButton *btn1=[uibutton buttonwithtype:uibuttontypecustom];    Btn1.frame=cgrectmake (30, 250, 200, 30);    [Btn1 Settitlecolor:[uicolor Redcolor] forstate:uicontrolstatenormal];    Btn1.backgroundcolor=[uicolor Graycolor];    Btn1.showstouchwhenhighlighted=yes;    [Btn1 settitle:@ "Stop" forstate:uicontrolstatenormal];    btn1.tag=1;        [Btn1 addtarget:self Action: @selector (Btnclick:) forcontrolevents:uicontroleventtouchupinside];    [Self.view ADDSUBVIEW:BTN1];    [Super Viewdidload]; Do any additional setup after loading the view, typically from a nib.} button to achieve: Click Stop, then the button becomes "start", the background green, and then the animation stops. Click Start, the button becomes "Stop", the background is gray.    The function of this button is actually similar to the play/Pause key-(void) Btnclick: (ID) sender{UIButton *btn= (UIButton *) sender; if ((int) btn.tag==1) {[btn settitle:@ ' Start ' forstate:uicontrolstatenormAL];        Btn.backgroundcolor=[uicolor Greencolor];        [ImgView2 stopanimating];    btn.tag=2;        }else{[btn settitle:@ "Stop" forstate:uicontrolstatenormal];        Btn.backgroundcolor=[uicolor Graycolor];        [ImgView2 startanimating];            btn.tag=1;    }}-(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end

Static effects:


Dynamic effects:


An introduction to the "iOS Dev-9" Picture property, which implements an animated effect similar to "click-to-Focus", and the way to implement a start/stop button toggle function

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.