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