仿支付宝网格移动
一直想做类似的效果,还是api懂得太少,参考大神的demo,做了简单的封装,基本还是大神的思想。贴上大神demo地址:GitHub - fuzheng0301/MoveButton: 仿支付宝首页按钮的点击、长按、删除、添加、移动位置功能
![](https://img.haomeiwen.com/i1694376/947662497539b6bd.gif)
button网格封装的核心代码:
//响应格子的长安手势事件
- (void)gridLongPress:(UILongPressGestureRecognizer *)longPressGesture
{
switch (longPressGesture.state) {
case UIGestureRecognizerStateBegan:
{
[self.delegate pressGestureStateBegan:longPressGesture withGridItem:self];
break;
}
case UIGestureRecognizerStateChanged:
{
//应用移动后的新坐标
CGPoint newPoint = [longPressGesture locationInView:longPressGesture.view];
[self.delegate pressGestureStateChangedWithPoint:newPoint grigItem:self];
break;
}
case UIGestureRecognizerStateEnded:
{
[self.delegate pressGestureStateEnded:self];
break;
}
default:
break;
}
}
//根据格子的坐标计算格子的索引位置
+ (NSInteger)indexOfPoint:(CGPoint)point
withButton:(UIButton *)btn
gridArray:(NSMutableArray *)gridListArray
{
for (NSInteger i = 0;i< gridListArray.count;i++)
{
UIButton *appButton = gridListArray[i];
if (appButton != btn)
{
if (CGRectContainsPoint(appButton.frame, point))
{
return i;
}
}
}
return -1;
}
只是研究的大神的思想,做了一层简单的封装,只需要用数组或者model初始化LX_GridView即可,重置下view的高度,实现两个代理方法即可,
demo 地址:网格View