1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4 {5UILabel *lable;6 BOOL Moveflag;7 nsinteger length;8UIButton *btn;9Nstimer *timer;Ten } One A @end - - @implementationViewcontroller the -- (void) Viewdidload { - [Super Viewdidload]; -Moveflag =NO; +Length =Ten; - +Self.view.backgroundColor =[Uicolor Darkgraycolor]; Alable =[[UILabel alloc] init]; atLable.frame = CGRectMake ( -, -,280, -); -Lable.text =@"dot me."; -Lable.backgroundcolor =[Uicolor Whitecolor]; - [Self.view addsubview:lable]; - - //button UIButton inBTN =[UIButton Buttonwithtype:uibuttontypecustom]; - //Set button coordinates, size toBtn.frame = CGRectMake ( the, -, -, -); + //Setting the background map -[Btn setimage:[uiimage imagenamed:@"Fire_down.png"] forstate:uicontrolstatenormal]; the[Btn setimage:[uiimage imagenamed:@"Blue_down.png"] forstate:uicontrolstatehighlighted]; * $ Panax Notoginseng //Set caption (status title) - //[btn settitle:@ "Start" forstate:uicontrolstatenormal]; the //[btn settitle:@ "Press" forstate:uicontrolstatehighlighted]; + A //add an [associated event] to a button the [btn addtarget:self Action: @selector (Touchme) forcontrolevents:uicontroleventtouchupinside]; + //Add a background color - [btn Setbackgroundcolor:[uicolor cyancolor]; $ $ //Button turns round -Btn.layer.cornerRadius = -; - //Add a button to the view the [Self.view addsubview:btn]; - Wuyi } the --(void) Touchme Wu { - //The ball didn't move when I judged it. About if(Moveflag = =NO) { $Moveflag =YES; - } - Else -Moveflag =NO; A + //when the ball is not moving, point the ball-change the if(moveflag) { -Timer = [Nstimer timerwithtimeinterval:0.1target:self selector: @selector (bollmove) Userinfo:nil Repeats:yes]; $ [[Nsrunloop Currentrunloop] Addtimer:timer formode:nsrunloopcommonmodes]; the } the Else the [timer invalidate]; the //when the ball moves, point the ball-don't move . - in } the the-(void) Bollmove About { the the if(btn.frame.origin.y> -) { theLength =-Ten; + - } the Else if(btn.frame.origin.y<0)Bayi { theLength =Ten; the } - -CGRect rect =Btn.frame; theRECT.ORIGIN.Y + =length; theBtn.frame =rect; the the}
IOS Ui-button button