2016.2.17(按钮拖动排序)

公司项目需要改一个按钮拖动排序模块,于是就自己写了一个,以为代码会很多,起始很少就实现,主要运用了长按手势,recognizer.state==UIGestureRecognizerStateChanged时判断选中按钮最后悬停的位置,然后依次改动起始位置到最后悬停位置的按钮的frame即可

- (void)longPress:(UILongPressGestureRecognizer*)recognizer

{

//NSLog(@"此方法在拖拽过程中会一直调用");

UIButton*selectedBtn = (UIButton*)recognizer.view;

CGPointlocation = [recognizerlocationInView:self.view];

if(recognizer.state==UIGestureRecognizerStateBegan) {

[UIViewanimateWithDuration:0.2animations:^{

selectedBtn.transform=CGAffineTransformMakeScale(1.2,1.2);

selectedBtn.alpha=0.5;

}];

//记录被拖拽按钮的当前位置

_valuePoint= selectedBtn.center;

}elseif(recognizer.state==UIGestureRecognizerStateChanged) {

selectedBtn.center= location;

for(UIButton*buttoninself.view.subviews) {

if(CGRectContainsPoint(button.frame, selectedBtn.center) && button != selectedBtn) {

NSIntegerfromIndex = selectedBtn.tag-_baseTag;

NSIntegertoIndex = button.tag-_baseTag;

NSLog(@"开始位置=%ld结束位置=%ld",fromIndex,toIndex);

//向后拖动

if(toIndex > fromIndex) {

[UIViewanimateWithDuration:0.2animations:^{

for(NSIntegeri = fromIndex +1; i <= toIndex; i++) {

UIButton*button = (UIButton*)[self.viewviewWithTag:i +_baseTag];

_nextPoint= button.center;

button.center=_valuePoint;

_valuePoint=_nextPoint;

button.tag--;

[buttonsetTitle:[NSStringstringWithFormat:@"第%ld个",button.tag-_baseTag]forState:UIControlStateNormal];

}

selectedBtn.tag=_baseTag+ toIndex;

}];

//向前拖动

}else{

[UIViewanimateWithDuration:0.2animations:^{

for(NSIntegeri = fromIndex -1; i >= toIndex; i--) {

UIButton*button = (UIButton*)[self.viewviewWithTag:i +_baseTag];

_nextPoint= button.center;

button.center=_valuePoint;

_valuePoint=_nextPoint;

button.tag++;

}

selectedBtn.tag=_baseTag+ toIndex;

[buttonsetTitle:[NSStringstringWithFormat:@"第%ld个",button.tag-_baseTag]forState:UIControlStateNormal];

}];

}

}

}

}elseif(recognizer.state==UIGestureRecognizerStateEnded) {

[UIViewanimateWithDuration:0.2animations:^{

selectedBtn.transform=CGAffineTransformIdentity;

selectedBtn.alpha=1.0;

selectedBtn.center=_valuePoint;

[selectedBtnsetTitle:[NSStringstringWithFormat:@"第%ld个",selectedBtn.tag-_baseTag]forState:UIControlStateNormal];

}];

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一:OC基础语法1 //整型 NSInteger a =10; //NSLog是OC里面的打印函数 NSLog(@...
    library阅读 3,081评论 0 3
  • 首先分为三种情况,分别是1.点击单个商品 2.点击商店选中该商店的所有商品 3.全选 分别创建按钮​ 单个商品的按...
    futur_zwl阅读 4,032评论 0 4
  • pop支持4种动画类型:弹簧动画效果、衰减动画效果、基本动画效果和自定义动画效果。 弹簧动画效果 1.效果图如下:...
    happyte阅读 8,712评论 0 13
  • “他从来都不能理解为什么我会有如此亲密的异性朋友,就像他从来不了解我内心的坚定。也许到现在,他仍然认为我在交往过程...
    ohnana阅读 2,900评论 0 0
  • 情人节快乐!你在哪
    ca1d51a3f3f2阅读 1,696评论 0 0