iOS view拖拽,靠边吸附悬停

最近项目需求:view 可以任意角度拖拽,并靠边吸附悬停
在项目中我使用了WMDragView ,一个拖拽很方便的封装的view
/**
是不是能拖曳,默认为YES
YES,能拖曳
NO,不能拖曳
/
@property (nonatomic,assign) BOOL dragEnable;
/
*
是不是总保持在父视图边界,默认为NO,没有黏贴边界效果
isKeepBounds = YES,它将自动黏贴边界,而且是最近的边界
isKeepBounds = NO, 它将不会黏贴在边界,它是free(自由)状态,跟随手指到任意位置,但是也不可以拖出给定的范围frame
/
@property (nonatomic,assign) BOOL isKeepBounds;
/
*
拖曳的方向,默认为any,任意方向
/
@property (nonatomic,assign) WMDragDirection dragDirection;
/
*
活动范围,默认为父视图的frame范围内(因为拖出父视图后无法点击,也没意义)
如果设置了,则会在给定的范围内活动
如果没设置,则会在父视图范围内活动
注意:设置的frame不要大于父视图范围
注意:设置的frame为0,0,0,0表示活动的范围为默认的父视图frame,如果想要不能活动,请设置dragEnable这个属性为NO
/
@property (nonatomic,assign) CGRect freeRect;
/
*
点击的回调block
/
@property (nonatomic,copy) void(^clickDragViewBlock)(WMDragView dragView);
/

开始拖动的回调block
/
@property (nonatomic,copy) void(^beginDragBlock)(WMDragView dragView);
/

拖动中的回调block
/
@property (nonatomic,copy) void(^duringDragBlock)(WMDragView dragView);
/

结束拖动的回调block
*/
@property (nonatomic,copy) void(^endDragBlock)(WMDragView *dragView);

项目中用到的拖拽代码实现:
WMDragView *redView = [[WMDragView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 150, self.tableView.bounds.size.height-227-20, 150, 227)];
self.redView = redView;
[self.view addSubview:redView];
[self.view insertSubview:redView aboveSubview:self.tableView];
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, redView.bounds.size.width, redView.bounds.size.height)];
bgImageView.contentMode = UIViewContentModeScaleAspectFill;
self.liveImageView = bgImageView;
[redView addSubview:bgImageView];

UIImageView *liveImaegeView = [[UIImageView alloc] initWithFrame:CGRectMake(6, 6, 53, 17)];
[redView addSubview:liveImaegeView];

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"live" ofType:@"gif"];
NSData *imageData = [NSData dataWithContentsOfFile:imagePath];
UIImage *image = [UIImage sd_imageWithGIFData:imageData];
liveImaegeView.image = image;
WeakSelf

///写拖拽点击事件
redView.clickDragViewBlock = ^(WMDragView *dragView) {
}

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

推荐阅读更多精彩内容