How iOS implements long press drag controls

Source: Internet
Author: User

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

Related Article

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.