最近項(xiàng)目需求:view 可以任意角度拖拽,并靠邊吸附懸停
在項(xiàng)目中我使用了WMDragView 匙奴,一個(gè)拖拽很方便的封裝的view
/**
是不是能拖曳堆巧,默認(rèn)為YES
YES,能拖曳
NO泼菌,不能拖曳
/
@property (nonatomic,assign) BOOL dragEnable;
/*
是不是總保持在父視圖邊界谍肤,默認(rèn)為NO,沒有黏貼邊界效果
isKeepBounds = YES,它將自動(dòng)黏貼邊界哗伯,而且是最近的邊界
isKeepBounds = NO荒揣, 它將不會(huì)黏貼在邊界,它是free(自由)狀態(tài)焊刹,跟隨手指到任意位置系任,但是也不可以拖出給定的范圍frame
/
@property (nonatomic,assign) BOOL isKeepBounds;
/*
拖曳的方向,默認(rèn)為any虐块,任意方向
/
@property (nonatomic,assign) WMDragDirection dragDirection;
/*
活動(dòng)范圍俩滥,默認(rèn)為父視圖的frame范圍內(nèi)(因?yàn)橥铣龈敢晥D后無法點(diǎn)擊,也沒意義)
如果設(shè)置了非凌,則會(huì)在給定的范圍內(nèi)活動(dòng)
如果沒設(shè)置举农,則會(huì)在父視圖范圍內(nèi)活動(dòng)
注意:設(shè)置的frame不要大于父視圖范圍
注意:設(shè)置的frame為0,0敞嗡,0颁糟,0表示活動(dòng)的范圍為默認(rèn)的父視圖frame,如果想要不能活動(dòng)喉悴,請(qǐng)?jiān)O(shè)置dragEnable這個(gè)屬性為NO
/
@property (nonatomic,assign) CGRect freeRect;
/*
點(diǎn)擊的回調(diào)block
/
@property (nonatomic,copy) void(^clickDragViewBlock)(WMDragView dragView);
/
開始拖動(dòng)的回調(diào)block
/
@property (nonatomic,copy) void(^beginDragBlock)(WMDragView dragView);
/
拖動(dòng)中的回調(diào)block
/
@property (nonatomic,copy) void(^duringDragBlock)(WMDragView dragView);
/
結(jié)束拖動(dòng)的回調(diào)block
*/
@property (nonatomic,copy) void(^endDragBlock)(WMDragView *dragView);
項(xiàng)目中用到的拖拽代碼實(shí)現(xiàn):
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
///寫拖拽點(diǎn)擊事件
redView.clickDragViewBlock = ^(WMDragView *dragView) {
}