下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。
内存溢出小编现在分享给大家,也给大家做个参考。
@interface UIDragbutton : UIbutton { CGPoint _prePoint; // 移动过程中的上一个点 BOol _isPress; // 是否按下:实现过程未用到 CGPoint _framePoint; // 未放大情况下frame的左上角坐标 CGRect _frameRect; // 未放大情况下frame值}@property (nonatomic, assign) NSMutableArray *buttonArray; // button集合@property (nonatomic, assign) NSInteger indexOfArray; // 当前按钮在集合中的下标// 移动动画,实现过程未用到,暂不能用- (voID)moveto:(CGRect)rect Animation:(BOol)flag;// 初始化一些样式,即长按手势- (voID)initStyle;@end
@implementation UIDragbutton/* * 样式初始化,即手势初始化 **/- (voID)initStyle { self.backgroundcolor = [UIcolor whitecolor]; self.layer.borderWIDth = 0.3; self.layer.bordercolor = [UIcolor graycolor].CGcolor; UILongPressGestureRecognizer * longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPresstodo:)]; longPressGr.minimumPressDuration = 1.0; [self addGestureRecognizer:longPressGr];}#pragma mark - 按钮尺寸更改// 放大- (voID)changeBig { self.transform = CGAffinetransformScale(self.transform,1.25,1.25); self.backgroundcolor = [UIcolor colorWithHexString:@"E0E0E0" Alpha:1];}// 还原- (voID)changeBack { self.transform = CGAffinetransformScale(self.transform,0.8,0.8); // 按钮放大还原后会有偏移,所以要设置回正常位置,80和100是按钮尺寸 self.frame = CGRectMake(_framePoint.x, _framePoint.y, 80, 100);}#pragma mark - 手势管理/* * 手势响应,并判断状态 **/- (voID)longPresstodo:(UILongPressGestureRecognizer *)press { if ([press state] == UIGestureRecognizerStateBegan) { [self begantouch:press]; [self changeBig]; // 放大 } else if ([press state] == UIGestureRecognizerStateChanged) { [self movedtouch:press]; } else if ([press state] == UIGestureRecognizerStateEnded){ [self endedtouch:press]; }}/* * 手势长按开始 **/- (voID)begantouch:(UILongPressGestureRecognizer *)press { _prePoint = [press locationInVIEw:self]; _frameRect = self.frame; _framePoint = self.frame.origin; // 该通知用于告诉controller点击的是第几个按钮,用于将该按钮放在最上层 [[NSNotificationCenter defaultCenter] postNotificationname:@"buttonChanged" object:[NSNumber numberWithInteger:_indexOfArray]];}/* * 手势长按结束 **/- (voID)endedtouch:(UILongPressGestureRecognizer *)press { _prePoint = [press locationInVIEw:self]; [self changeBack]; self.backgroundcolor = [UIcolor whitecolor];}/* * 长按过程中移动 **/- (voID)movedtouch:(UILongPressGestureRecognizer *)press { CGPoint Now = [press locationInVIEw:self]; NSInteger x = Now.x - _prePoint.x; NSInteger y = Now.y - _prePoint.y; self.frame = CGRectMake(self.frame.origin.x+x, self.frame.origin.y+y, self.frame.size.wIDth, self.frame.size.height); [self placeIsChanged:CGPointMake(self.frame.origin.x + _prePoint.x, self.frame.origin.y + _prePoint.y)];}#pragma mark - 按钮调整/* * 判断当前按钮位置是否变化,并进行调整 **/- (voID)placeIsChanged:(CGPoint)point { for (NSInteger i = 0; i < [self.buttonArray count]; i++) { if ([self pointIn:point rect:((UIbutton *)[_buttonArray objectAtIndex:i]).frame] && _indexOfArray != i) { [self adjustbuttons:i]; } }}/* * 判断点是否在rect内 **/- (BOol)pointIn:(CGPoint)point rect:(CGRect)rect { if (point.x > rect.origin.x && point.y > rect.origin.y && point.x < rect.origin.x+rect.size.wIDth && point.y < rect.origin.y+rect.size.height) { return YES; } return NO;}/* * 调整按钮位置 **/- (voID)adjustbuttons:(NSInteger)index { CGRect rect = ((UIbutton *)[_buttonArray objectAtIndex:index]).frame; __block NSInteger i = 0; __block NSInteger num = index; // 将靠前的按钮移动到靠后的位置 if (_indexOfArray < index) { // 将上一个按钮的位置赋值给当前按钮 [UIVIEw animateWithDuration:0.5 animations:^{ for (i = num; i > _indexOfArray+1; i--) { ((UIDragbutton *)[_buttonArray objectAtIndex:i]).frame = ((UIDragbutton *)[_buttonArray objectAtIndex:i-1]).frame; } ((UIDragbutton *)[_buttonArray objectAtIndex:i]).frame = _frameRect; }]; _frameRect = rect; // 调整顺序 保证数组中按钮的frame按顺序排列 for (i = _indexOfArray; i < index; i++) { [_buttonArray exchangeObjectAtIndex:i withObjectAtIndex:i+1]; ((UIDragbutton *)[_buttonArray objectAtIndex:i]).indexOfArray = i; } ((UIDragbutton *)[_buttonArray objectAtIndex:index]).indexOfArray = index; } else { // 将靠后的按钮移动到前边 // 将上一个按钮的位置赋值给当前按钮 [UIVIEw animateWithDuration:0.5 animations:^{ for (i = num; i < _indexOfArray-1; i++) { ((UIDragbutton *)[_buttonArray objectAtIndex:i]).frame = ((UIDragbutton *)[_buttonArray objectAtIndex:i+1]).frame; } ((UIDragbutton *)[_buttonArray objectAtIndex:i]).frame = _frameRect; }]; _frameRect = rect; // 调整顺序 保证数组中按钮的frame按顺序排列 for (i = _indexOfArray; i > index; i--) { [_buttonArray exchangeObjectAtIndex:i withObjectAtIndex:i-1]; ((UIDragbutton *)[_buttonArray objectAtIndex:i]).indexOfArray = i; } ((UIDragbutton *)[_buttonArray objectAtIndex:index]).indexOfArray = index; } _framePoint = _frameRect.origin;}#pragma mark - 按钮移动动画- (voID)moveto:(CGRect)rect Animation:(BOol)flag { if (flag) { // 计算偏移并移动 float x = rect.origin.x - self.frame.origin.x; float y = rect.origin.y - self.frame.origin.y; [self.layer addAnimation:[self moveX:0.1 X:[NSNumber numberWithfloat:x]] forKey:nil]; [self.layer addAnimation:[self moveY:0.1 Y:[NSNumber numberWithfloat:y]] forKey:nil]; } else { self.frame = rect; }}- (CABasicAnimation *)moveX:(float)time X:(NSNumber *)x // 横向移动{ CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.x"]; animation.tovalue=x; animation.duration=time; // 动画持续时间 animation.removedOnCompletion=NO; animation.fillMode=kCAFillModeForwards; return animation;}- (CABasicAnimation *)moveY:(float)time Y:(NSNumber *)y // 纵向移动{ CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.translation.y"]; animation.tovalue=y; animation.duration=time; animation.removedOnCompletion=NO; animation.fillMode=kCAFillModeForwards; return animation;}@end
以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
总结以上是内存溢出为你收集整理的iOS 支付宝首页拖放按钮效果实现全部内容,希望文章能够帮你解决iOS 支付宝首页拖放按钮效果实现所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)