Play the animation after uibutton is pressed down in IOS. The animation disappears after it is released.

Source: Internet
Author: User

Requirement: There is a button on the page. The user requires that when the finger presses this button, a circle appears around this button. When the finger is released, the animation disappears.

I will not describe the specific page layout and event binding methods. Here, we only want to talk about the uibutton event that needs help. The method to be pressed is to bind the uibutton event.Touch down, The uibutton event to bind to the released method isTouch up insideAndTouch up outside(If you want to cancel playing the animation when your finger leaves the button, you need to bindTouch drag outsideEvent)

SpecificCodeAs follows:

 1 -( Void ) Pressedevent :( ID ) Sender { //  Response Method for pressing event of Button  2 Uibutton * BTN = Sender;  3 Nsarray * imgarray = [nsarray arraywithobjects: [uiimage imagenamed: @"  Anim_0  " ], [Uiimage imagenamed: @"  Anim_1  " ], [Uiimage imagenamed: @"  Anim_2  " ], [Uiimage imagenamed: @"  Anim_3  " ], Nil]; //  Defines the number of frames of an animation. 4 Uiimageview * animimgview = [[uiimageview alloc] init]; //  Initialize a uiimageview to play our animation frame by frame.  5 Animimgview. Frame = cgrectmake ( 0 , 0 , (Uiimage *) [imgarray objectatindex: 0 ]). Size. Width, (uiimage *) [imgarray objectatindex: 0 ]). Size. Height ); //  By default, the size of each animation frame is the same. The size of the first image is taken as the size of the uiimageview.  6 Animimgview. Center = BTN. Center;//  The above figure only shows the size of uiimageview. Here, we set the placement position to overlap the center of the animation and the center of the button.  7 Animimgview. Tag = 10000 ; //  This setting is used to obtain the uiimageview of the playback animation from the release action triggered by the pressed button.  8 Animimgview. animationimages = imgarray; //  Pass the frame-by-frame animation array to uiimageview  9 Animimgview. animationduration = 1 ; //  Time used to browse all images 10 Animimgview. animationrepeatcount = 0 ; //  0 = number of loops forever animation repetitions  11 [Animimgview startanimating]; //  Start playing the animation  12 [Self. View addsubview: animimgview]; //  Add view to form  13 [Self. View sendsubviewtoback: animimgview]; //  Move the animation playback View to the bottom of the elf. View. Pay attention to the layer occlusion problem. 14   [Animimgview release];  15   }  16 -( Void ) Unpressedevent :( ID ) Sender { //  Button release Event Response Method  17 [[Self. View viewwithtag: 10000  ] Removefromsuperview];  18 }
Related Article

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.