There are several ways to implement a control drag, which can be implemented directly using the Uicollectionview proxy method, but some developers do not use Uicollectionview to create nine Gongge at the beginning, and then increase the demand, but increase the effect of this drag movement. Do not want to change the initial control of the page, then how should be implemented?
The method is very simple, first create the following global variables in @interface;
@interface Yrviewcontroller () { BOOL contain; Cgpoint StartPoint; Cgpoint originpoint; *ItemArray; @end
, the following layout of controls was created in Viewdidload;
1- (void) Viewdidload2 {3 [Super Viewdidload];4 5 for(Nsinteger i =0;i<8; i++)6 {7UIButton *btn =[UIButton Buttonwithtype:uibuttontypecustom];8Btn.backgroundcolor =[Uicolor Redcolor];9Btn.frame = CGRectMake ( -+ (i%2)* -, -+ (i/2)* -, -, -);TenBtn.tag =i; OneBtn.titleLabel.font = [Uifont boldsystemfontofsize: -]; A[Btn settitle:[nsstring stringWithFormat:@"%d",1+i] forstate:uicontrolstatenormal]; - [Self.view addsubview:btn]; -Uilongpressgesturerecognizer *longgesture =[[Uilongpressgesturerecognizer alloc] initwithtarget:self action: @selector (buttonlongpressed:)]; the [btn Addgesturerecognizer:longgesture]; - [Self.itemarray addobject:btn]; - - } +}
Below, we are going to think about how to implement long press move, control move and reorder, mainly consider moving the control to move to a new location, directly look at the following code,
-(void) buttonlongpressed: (Uilongpressgesturerecognizer *) sender{UIButton *btn = (UIButton *) Sender.view; if (sender.state = = Uigesturerecognizerstatebegan) {startPoint = [sender LocationInView:sender.view]; Originpoint = Btn.center; [UIView animatewithduration:duration animations:^{btn.transform = Cgaffinetransformmakescale (1.1, 1.1); Btn.alpha = 0.7; }]; } else if (sender.state = = uigesturerecognizerstatechanged) {cgpoint newpoint = [Sender Locationinvi Ew:sender.view]; CGFloat deltax = newpoint.x-startpoint.x; CGFloat deltay = Newpoint.y-startpoint.y; Btn.center = Cgpointmake (Btn.center.x+deltax,btn.center.y+deltay); NSLog (@ "center =%@", Nsstringfromcgpoint (Btn.center)); Nsinteger index = [self indexOfPoint:btn.center withbutton:btn]; if (index<0) {contain = NO; } else {[UIView animAtewithduration:duration animations:^{Cgpoint temp = Cgpointzero; UIButton *button = _itemarray[index]; temp = Button.center; Button.center = Originpoint; Btn.center = temp; Originpoint = Btn.center; contain = YES; }]; }} else if (sender.state = = uigesturerecognizerstateended) {[UIView animatewithduration: Duration animations:^{btn.transform = cgaffinetransformidentity; Btn.alpha = 1.0; if (!contain) {btn.center = Originpoint; } }]; }}-(Nsinteger) Indexofpoint: (cgpoint) point Withbutton: (UIButton *) btn{for (Nsinteger i = 0;i<_itemarray.count;i++) {UIButton *button = _itemarray[i]; if (Button! = btn) {if (Cgrectcontainspoint (Button.frame, point)) {return i; }}} return-1;}
This allows the control to be rearranged in a way that determines the location.
How iOS implements long press drag controls