您的位置:首页 > 其它

tableViewcell上进行手势的添加

2015-10-20 15:27 441 查看
未进行操作时是这样的:



轻扫或者点击之后为这样的:



self.isLeft = NO;//默认为左边按钮是不出现的状态

//编辑按钮
self.editButton = [[UIButton alloc] initWithFrame:CGRectMake(BOUNDS.size.width-46, 0, 46, 43)];
[_editButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
[_editButton addTarget:self action:@selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:_editButton];


//删除按钮
self.deleteBtn = [[UIButton alloc] initWithFrame:CGRectMake(_editButton.frame.origin.x, 47, _editButton.frame.size.width, _editButton.frame.size.height)];
[_deleteBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
[_deleteBtn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
[self.contentView addSubview:_deleteBtn];


//向左轻扫
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftRecognizerThisCell:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.subView addGestureRecognizer:leftRecognizer];

//向右轻扫
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightRecognizerThisCell:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[self.subView addGestureRecognizer:rightRecognizer];

//点击
UITapGestureRecognizer *pan = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(panRecognizerThisCell:)];
[self.subView addGestureRecognizer:pan];


//手势的触发

//向左轻扫时手势添加的这个view左移动
- (void)leftRecognizerThisCell:(UIGestureRecognizer *)gesture
{
UIView *view = gesture.view;
[UIView animateWithDuration:0.2 animations:^{
view.frame = CGRectMake(-46, _subView.frame.origin.y, _subView.frame.size.width, _subView.frame.size.height);
_isLeft = YES;
}];
}

//向右轻扫时手势添加的这个view右移动
- (void)rightRecognizerThisCell:(UIGestureRecognizer *)gesture
{
UIView *view = gesture.view;
if (_isLeft) {
[UIView animateWithDuration:0.2 animations:^{
view.frame = CGRectMake(0, 0, _subView.frame.size.width, _subView.frame.size.height);
_isLeft = NO;
}];
}
}

//点击时,先判断此时的状态
- (void)panRecognizerThisCell:(UIPanGestureRecognizer *)gesture
{
if (_isLeft) {
[self rightRecognizerThisCell:gesture];
} else {
[self leftRecognizerThisCell:gesture];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: