IOS UIButton setting up picture animations

Source: Internet
Author: User

1. Problem Description: Implement click button to play MP3 audio and turn on the animation, then click the button to close the animation and audio

The effect is similar to the following (Pictures from the network, Invasion and deletion), the animation effect is actually several consecutive picture composition:

2. Realization of Ideas

2.1 Customize view, set ImageView animation and add view's click Gesture to control animation playback, end;

2.2 Directly customize a button, set the button's ImageView property implementation, it is more simple;

3. Implementing the code (using the second method)

Customizing a UIButton, such as Animateimgbutton, implements the method

. m

//Customize the button code. M- (ID) Initwithcoder: (Nscoder *) adecoder{if(self =[Super Initwithcoder:adecoder])    {[Self commoninit]; }    returnSelf ;}- (ID) initWithFrame: (cgrect) frame{if(self =[Super Initwithframe:frame])    {[Self commoninit]; }    returnSelf ;}- (void) commoninit{}-(CGRect) Imagerectforcontentrect: (cgrect) bounds{//Override Imagerectforcontentrect method returns the bounds of the button, otherwise the picture size cannot be controlled, Btn.imageview Setcontentmode:uiviewcontentmodescaleaspectfill]; Tried to add invalidreturnCGRectMake (0.0,0.0, Self.size.width, self.size.height);}//Repeating animations- (void) Startimganimatewithimgarr: (Nsarray *) Imgarr time: (cgfloat) time{[Self.imageview Setanimationimages:imgarr]; Self.imageView.animationDuration=Time ; Self.imageView.animationRepeatCount=0; Self.isplayanimate=YES; if(!self.imageView.isAnimating) {[Self.imageview startanimating]; }    }- (void) stopimganimate{self.isplayanimate=NO;    [Self.imageview stopanimating]; Self.imageView.animationImages=Nil;}

. h

/**/@property (nonatomic, assign) BOOL isplayanimate; // Repeat animation -(void) Startimganimatewithimgarr: (Nsarray *) Imgarr time: (cgfloat) time; -(void) stopimganimate;

Call:

-(void) Btnanimateclick: (Animateimgbutton *) sender{
if (sender.isplayanimate) {
NSLog (@ "Close button animation");
[Sender stopimganimate];
}else{
NSLog (@ "Turn on button animation");
[Self.btnanimate STARTIMGANIMATEWITHIMGARR:SELF.IMGARR time:1];
}
}


-(Animateimgbutton *) btnanimate{if(!_btnanimate) {_btnanimate= [AnimateimgbuttonNew]; _btnanimate.isplayanimate=NO; [_btnanimate Setimage:kimage (@"IMGVoice3") Forstate:uicontrolstatenormal]; [_btnanimate addtarget:self Action: @selector (Btnanimateclick:) forcontrolevents:uicontroleventtouchupinside]; } return_btnanimate;}-(Nsarray *) imgarr{if(!_imgarr) {_imgarr=[Nsarray arraywithobjects: [UIImage imagenamed:@"IMGVoice1"], [UIImage imagenamed:@"IMGVoice2"], [UIImage imagenamed:@"IMGVoice3"],nil]; } return_imgarr;}

IOS UIButton setting up picture animations

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.