描述:项目中android用到的滑动删除。网上没找到类似就自己做了一个。
思路:
思路1.每个cell里面加一个scrollView 这个可能消耗太大没尝试。
思路2.给cell中添加pan手势
完成中遇到的问题与长进:
1.复习了block与table那套轻量级的绑定 但是发现具体问题出现时需要修改的还是不少。
2.可以通过设置gesture的delegate可以控制什么时候捕获手势。如ex1
ex1:
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
CGPoint velocityPoint = [gestureRecognizer velocityInView:self];
if (fabsf(velocityPoint.x) > 100.0) {
return YES;
}else
return NO;
}else
return NO;
}
//
// PLLSilderToDelete.m
// SliderToDeleteDemo
//
// Created by liu poolo on 14-7-29.
// Copyright (c) 2014年 liu poolo. All rights reserved.
//
#import "PLLSilderToDelete.h"
@implementation PLLSilderToDelete(cell_sliderDeleSetValuePro)
- (void)configureForPLSetValueProtocol:(id<PLLSliderDeleteSetValuePro>) p{
self.hideLabel.text=[p backgroundStr];
self.topLabel.text=[p topLabelStr];
self.cellView.backgroundColor=[p color];
}
@end
@interface PLLSilderToDelete(){
CGPoint defalutCenter;
}
@end
@implementation PLLSilderToDelete
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[[NSBundle mainBundle]loadNibNamed:@"sliderDemo" owner:self options:nil];
[self.contentView addSubview:self.cellView];
UIPanGestureRecognizer* pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
pan.delegate=self;
[self.topContentView addGestureRecognizer:pan];
}
return self;
}
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
CGPoint velocityPoint = [gestureRecognizer velocityInView:self];
if (fabsf(velocityPoint.x) > 100.0) {
return YES;
}else
return NO;
}else
return NO;
}
-(void)handlePan:(UIPanGestureRecognizer*)gesture{
NSLog(@"%@", self.hideLabel);
if(gesture.state==UIGestureRecognizerStateBegan){
NSLog(@"begin");
defalutCenter=self.topContentView.center;
}else if(gesture.state==UIGestureRecognizerStateChanged){
CGPoint translation=[gesture translationInView:gesture.view.superview];
if(translation.x<0){
self.topContentView.center=CGPointMake(defalutCenter.x+translation.x, defalutCenter.y);
float alpha=1.0-(translation.x/self.cellView.frame.size.width);
self.topContentView.alpha=alpha;
}
}else if(gesture.state==UIGestureRecognizerStateEnded){
CGPoint translation=[gesture translationInView:gesture.view.superview];
if (translation.x<-50) {
[UIView animateWithDuration:0.3 animations:^{
self.topContentView.frame=CGRectMake(-self.topContentView.frame.size.width, self.topContentView.frame.origin.y,self.topContentView.frame.size.width, self.topContentView.frame.size.height);
} completion:^(BOOL finished) {
[self.delegate silderDoWithIndex:self.contentView.tag];
self.topContentView.frame=CGRectMake(0, self.topContentView.frame.origin.y,self.topContentView.frame.size.width, self.topContentView.frame.size.height);
self.topContentView.alpha=1.0f;
}];
}else if(gesture.state==UIGestureRecognizerStateEnded){
[UIView animateWithDuration:0.3 animations:^{
self.topContentView.frame=CGRectMake(0, self.topContentView.frame.origin.y,self.topContentView.frame.size.width, self.topContentView.frame.size.height);
self.topContentView.alpha=1.0f;
} completion:nil];
}
}
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
效果图:
思路:
思路1.每个cell里面加一个scrollView 这个可能消耗太大没尝试。
思路2.给cell中添加pan手势
完成中遇到的问题与长进:
1.复习了block与table那套轻量级的绑定 但是发现具体问题出现时需要修改的还是不少。
2.可以通过设置gesture的delegate可以控制什么时候捕获手势。如ex1
ex1:
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
CGPoint velocityPoint = [gestureRecognizer velocityInView:self];
if (fabsf(velocityPoint.x) > 100.0) {
return YES;
}else
return NO;
}else
return NO;
}
//
// PLLSilderToDelete.m
// SliderToDeleteDemo
//
// Created by liu poolo on 14-7-29.
// Copyright (c) 2014年 liu poolo. All rights reserved.
//
#import "PLLSilderToDelete.h"
@implementation PLLSilderToDelete(cell_sliderDeleSetValuePro)
- (void)configureForPLSetValueProtocol:(id<PLLSliderDeleteSetValuePro>) p{
self.hideLabel.text=[p backgroundStr];
self.topLabel.text=[p topLabelStr];
self.cellView.backgroundColor=[p color];
}
@end
@interface PLLSilderToDelete(){
CGPoint defalutCenter;
}
@end
@implementation PLLSilderToDelete
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
[[NSBundle mainBundle]loadNibNamed:@"sliderDemo" owner:self options:nil];
[self.contentView addSubview:self.cellView];
UIPanGestureRecognizer* pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
pan.delegate=self;
[self.topContentView addGestureRecognizer:pan];
}
return self;
}
- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer{
if ([gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
CGPoint velocityPoint = [gestureRecognizer velocityInView:self];
if (fabsf(velocityPoint.x) > 100.0) {
return YES;
}else
return NO;
}else
return NO;
}
-(void)handlePan:(UIPanGestureRecognizer*)gesture{
NSLog(@"%@", self.hideLabel);
if(gesture.state==UIGestureRecognizerStateBegan){
NSLog(@"begin");
defalutCenter=self.topContentView.center;
}else if(gesture.state==UIGestureRecognizerStateChanged){
CGPoint translation=[gesture translationInView:gesture.view.superview];
if(translation.x<0){
self.topContentView.center=CGPointMake(defalutCenter.x+translation.x, defalutCenter.y);
float alpha=1.0-(translation.x/self.cellView.frame.size.width);
self.topContentView.alpha=alpha;
}
}else if(gesture.state==UIGestureRecognizerStateEnded){
CGPoint translation=[gesture translationInView:gesture.view.superview];
if (translation.x<-50) {
[UIView animateWithDuration:0.3 animations:^{
self.topContentView.frame=CGRectMake(-self.topContentView.frame.size.width, self.topContentView.frame.origin.y,self.topContentView.frame.size.width, self.topContentView.frame.size.height);
} completion:^(BOOL finished) {
[self.delegate silderDoWithIndex:self.contentView.tag];
self.topContentView.frame=CGRectMake(0, self.topContentView.frame.origin.y,self.topContentView.frame.size.width, self.topContentView.frame.size.height);
self.topContentView.alpha=1.0f;
}];
}else if(gesture.state==UIGestureRecognizerStateEnded){
[UIView animateWithDuration:0.3 animations:^{
self.topContentView.frame=CGRectMake(0, self.topContentView.frame.origin.y,self.topContentView.frame.size.width, self.topContentView.frame.size.height);
self.topContentView.alpha=1.0f;
} completion:nil];
}
}
}
- (void)awakeFromNib
{
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end