iOS开发给UITableView的单元格做一个类似于QQ和微信的侧滑露出删除按钮的思路
2017-05-15 15:58
447 查看
可以在tableView的cell的contentView的的下方添加删除和编辑按钮,然后给cell的contentView添加平移手势,滑动时候让cell的contentView左滑从而露出后面的删除和别的按钮
具体代码:
1.awakeFromNib方法中添加按钮和手势
- (void)awakeFromNib {
[super
awakeFromNib];
// Initialization code
_deleteBtn = [[UIButton
alloc]initWithFrame:CGRectMake(kDeviceWidth-70,
0, 70,
60)];
[_deleteBtn
setTitle:@"删除"
forState:UIControlStateNormal];
_deleteBtn.backgroundColor = [UIColor
redColor];
[_deleteBtn
addTarget:self
action:@selector(deleteBtnAct:)
forControlEvents:UIControlEventTouchUpInside];
[self
insertSubview:_deleteBtn
belowSubview:self.contentView];
_editBtn = [[UIButton
alloc]initWithFrame:CGRectMake(kDeviceWidth-140,
0, 70,
60)];
[_editBtn
setTitle:@"编辑"
forState:UIControlStateNormal];
_editBtn.backgroundColor = [UIColor
grayColor];
[_editBtn
4000
addTarget:self
action:@selector(editBtnAct:)
forControlEvents:UIControlEventTouchUpInside];
[self
insertSubview:_editBtn
belowSubview:self.contentView];
//.平移手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer
alloc]initWithTarget:self
action:@selector(panAct:)];
pan.delegate =
self;
pan.delaysTouchesBegan =
YES;
//添加平移手势
[self.contentView
addGestureRecognizer:pan];
}
平移方法
//平移方法
- (void)panAct:(UIPanGestureRecognizer *)pan{
CGPoint point = [pan
translationInView:pan.view];
CGFloat value = point.x;
if (self.contentView.left < -(70
+ 70) * 1.1 ||
self.contentView.left >
30) {
value = 0;
}
self.contentView.left += value;
if (pan.state ==
UIGestureRecognizerStateEnded) {
CGFloat x =
0;
if (self.contentView.left
< -30 && !self.isSlided) {
x = -140;
self.isSlided =
YES;
}else{
self.isSlided =
NO;
}
[UIView
animateWithDuration:0.4
delay:0
usingSpringWithDamping:0.6
initialSpringVelocity:2
options:UIViewAnimationOptionLayoutSubviews
animations:^{
self.contentView.left = x;
} completion:^(BOOL finished) {
}];
}
[pan setTranslation:CGPointZero
inView:pan.view];
}
具体代码:
1.awakeFromNib方法中添加按钮和手势
- (void)awakeFromNib {
[super
awakeFromNib];
// Initialization code
_deleteBtn = [[UIButton
alloc]initWithFrame:CGRectMake(kDeviceWidth-70,
0, 70,
60)];
[_deleteBtn
setTitle:@"删除"
forState:UIControlStateNormal];
_deleteBtn.backgroundColor = [UIColor
redColor];
[_deleteBtn
addTarget:self
action:@selector(deleteBtnAct:)
forControlEvents:UIControlEventTouchUpInside];
[self
insertSubview:_deleteBtn
belowSubview:self.contentView];
_editBtn = [[UIButton
alloc]initWithFrame:CGRectMake(kDeviceWidth-140,
0, 70,
60)];
[_editBtn
setTitle:@"编辑"
forState:UIControlStateNormal];
_editBtn.backgroundColor = [UIColor
grayColor];
[_editBtn
4000
addTarget:self
action:@selector(editBtnAct:)
forControlEvents:UIControlEventTouchUpInside];
[self
insertSubview:_editBtn
belowSubview:self.contentView];
//.平移手势
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer
alloc]initWithTarget:self
action:@selector(panAct:)];
pan.delegate =
self;
pan.delaysTouchesBegan =
YES;
//添加平移手势
[self.contentView
addGestureRecognizer:pan];
}
平移方法
//平移方法
- (void)panAct:(UIPanGestureRecognizer *)pan{
CGPoint point = [pan
translationInView:pan.view];
CGFloat value = point.x;
if (self.contentView.left < -(70
+ 70) * 1.1 ||
self.contentView.left >
30) {
value = 0;
}
self.contentView.left += value;
if (pan.state ==
UIGestureRecognizerStateEnded) {
CGFloat x =
0;
if (self.contentView.left
< -30 && !self.isSlided) {
x = -140;
self.isSlided =
YES;
}else{
self.isSlided =
NO;
}
[UIView
animateWithDuration:0.4
delay:0
usingSpringWithDamping:0.6
initialSpringVelocity:2
options:UIViewAnimationOptionLayoutSubviews
animations:^{
self.contentView.left = x;
} completion:^(BOOL finished) {
}];
}
[pan setTranslation:CGPointZero
inView:pan.view];
}
相关文章推荐
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
- iOS中给UITableView的侧滑删除增加多个按钮的实现方法
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)
- iOS开发学习之利用系统远程UITableView 自定义滑动删除按钮、增加自定义滑动按钮方法
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
- iOS开发UI基础—28使用UItableview完成一个简单的QQ好友列表(二)
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)
- IOS 开发使用UITableView 实现滑动 删除等多个按钮
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(二)
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
- iOS开发UI篇章使用UItableview完成一个简单的QQ好友列表(一)
- iOS开发UI基础—27使用UItableview完成一个简单的QQ好友列表(一)
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)
- iOS开发:UITableView实现侧滑删除cell的功能
- iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)