This article is a piece of my "iOS development Daily Small Notes" series, which is recorded in the development work today, can be explained with a short article or a small demo demo small tips. They may give the user experience, the code efficiency to get some improvement, or have not been in touch with the technology, very happy to learn, put here to be a little bit. The role of 90% is to help you review, remember, review.
Originally thought National Day holiday can have time to read books, write blog. In fact, most of the time was Gan Chang to attend the wedding and go to relatives around the house to eat the Occupy. See Tomorrow is still the last day time, today hurriedly to update an article, also is not completely abandoned this 7 days long vacation bar!
Facebook's client login input box, there is a very beautiful effect, that is, when the user entered the user name and password information is wrong, the entire input interface will shake around, prompting the user "log in error", very interesting. In fact, this effect is very simple to achieve. Here are two ideas to share.
First look at the effect, when you click on the button below, the gray UIView will sway around.
(GIF picture is not very good, the real machine run out to be better.) )
Both ideas are implemented using caanimation, but one way to set the path for caanimation using the draw path, and the other is to use "Keyframe" to set value.
1.1, Path method:
1 //2 //VIEWCONTROLLER.M3 //Shakeinputviewdemo4 //5 //Created by Pigpigdaddy on 14-10-6.6 //Copyright (c) 2014 Pigpigdaddy. All rights reserved.7 //8 9 #import "ViewController.h"Ten One @interfaceViewcontroller () A -@property (nonatomic, strong) UIView *Shakeview; -@property (nonatomic, strong) UIButton *button; the - @end - - @implementationViewcontroller + -- (ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil + { ASelf =[Super Initwithnibname:nibnameornil Bundle:nibbundleornil]; at if(self) { - //Custom Initialization - } - returnSelf ; - } - in- (void) Viewdidload - { to [Super Viewdidload]; + //Do any additional setup after loading the view. - theSelf.button =[UIButton Buttonwithtype:uibuttontypesystem]; *[Self.button Settitle:@"left and right jitter"Forstate:uicontrolstatenormal]; $Self.button.frame = CGRectMake ( -, -, -, +);Panax Notoginseng [Self.button addtarget:self Action: @selector (Shakeaction) forcontrolevents:uicontroleventtouchupinside]; - [Self.view AddSubview:self.button]; the +Self.shakeview = [[UIView alloc] Initwithframe:cgrectmake ( -, the, -, the)]; ASelf.shakeView.backgroundColor =[Uicolor Graycolor]; the [Self.view AddSubview:self.shakeView]; + } - $- (void) didreceivememorywarning $ { - [Super didreceivememorywarning]; - //Dispose of any resources the can be recreated. the } - Wuyi /* the #pragma mark-navigation - Wu //In a storyboard-based application, you'll often want to do a little preparation before navigation - -(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) Sender About { $ //Get The new view controller using [Segue Destinationviewcontroller]. - //Pass The selected object to the new view controller. - } - */ A +- (void) Shakeaction the { - //Number of Shakes $ Static intNumberofshakes =4; the //shaking amplitude (relative to total width) the Static floatVigourofshake =0.04f; the //The shaking continues often (seconds) the Static floatDurationofshake =0.5f; - inCakeyframeanimation *shakeanimation = [cakeyframeanimation animationwithkeypath:@"position"]; the the //method One: Draw the path AboutCGRect frame =Self.shakeView.frame; the //Create Path theCgmutablepathref Shakepath =cgpathcreatemutable (); the //starting point + Cgpathmovetopoint (Shakepath, NULL, Cgrectgetmidx (frame), Cgrectgetmidy (frame)); - for(intindex =0; Index < Numberofshakes; index++) the {Bayi //Add rocking path amplitude from big to small theCgpathaddlinetopoint (Shakepath, NULL, Cgrectgetmidx (frame)-Frame.size.width * vigourofshake* (1-(float) index/numberofshakes), Cgrectgetmidy (frame)); theCgpathaddlinetopoint (Shakepath, NULL, Cgrectgetmidx (frame) + Frame.size.width * vigourofshake* (1-(float) index/numberofshakes), Cgrectgetmidy (frame)); - } - //closed the Cgpathclosesubpath (shakepath); theShakeanimation.path =Shakepath; theShakeanimation.duration =Durationofshake; the //Release - cfrelease (shakepath); the the [Self.shakeView.layer addanimation:shakeanimation forkey:kcatransition]; the}
1.2, set "Keyframe", let caanimation automatically "filled" the animation between frame and frame, just change the following function:
1- (void) Shakeaction2 {3 //Number of Shakes4 Static intNumberofshakes =4;5 //shaking amplitude (relative to total width)6 Static floatVigourofshake =0.04f;7 //The shaking continues often (seconds)8 Static floatDurationofshake =0.5f;9 TenCakeyframeanimation *shakeanimation = [cakeyframeanimation animationwithkeypath:@"position"]; One A //method Two: Keyframe (point) -Cgpoint layerposition =self.shakeView.layer.position; - the //starting point -Nsvalue *value1=[Nsvalue valueWithCGPoint:self.shakeView.layer.position]; - //Key points Group -Nsmutablearray *values =[[Nsmutablearray alloc] initwithobjects:value1, nil]; + for(inti =0; i<numberofshakes; i++) { - //the point of shaking left and right +Nsvalue *valueleft = [Nsvalue valuewithcgpoint:cgpointmake (layerposition.x-self.shakeview.frame.size.width* Vigourofshake* (1-(float) i/numberofshakes), LAYERPOSITION.Y)]; ANsvalue *valueright = [Nsvalue valuewithcgpoint:cgpointmake (layerposition.x+self.shakeview.frame.size.width* Vigourofshake* (1-(float) i/numberofshakes), LAYERPOSITION.Y)]; at - [Values Addobject:valueleft]; - [Values addobject:valueright]; - } - //finally return to the starting point - [Values addobject:value1]; in -Shakeanimation.values =values; toShakeanimation.duration =Durationofshake; + - the [Self.shakeView.layer addanimation:shakeanimation forkey:kcatransition]; *}
The effect of both methods is the same.
Demo:https://github.com/pigpigdaddy/shakeinputviewdemo
"iOS Development Daily Small notes (12)" Fake Facebook Login interface Error alert jitter use Caanimation to animate