产品经理让做一个仿饿了么底部菜单刚开始显示一部分,上拉时全部显示出来,向下滑时底部菜单再向下滑,只显示一部分,安卓有线程系统提供的,但是iOS系统没有,于是自己写了一个吧,供大家参考使用。
这是m文件内容,h文件内容就不写了,啥都没有
#import "HICDragView.h"
@interface HICDragView()
@property (nonatomic,strong)UIPanGestureRecognizer *pan;
@end
@implementation HICDragView
-(instancetype)init{
if ([super init]) {
[self setupPanGesture];
}
return self;
}
-(instancetype)initWithFrame:(CGRect)frame{
if ([super initWithFrame:frame]) {
[self setupPanGesture];
}
return self;
}
-(void)setupPanGesture{
_pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
_pan.minimumNumberOfTouches = 1;
[self addGestureRecognizer:_pan];
}
- (void)panAction:(UIPanGestureRecognizer *)pan{
CGPoint point = [pan translationInView:pan.view];
if (pan.state == UIGestureRecognizerStateFailed || pan.state == UIGestureRecognizerStateEnded) {
if (pan.view.top >= SCREEN_HEIGHT-395 && pan.view.top <= SCREEN_HEIGHT-395+130) {
[UIView animateWithDuration:0.1 animations:^{
pan.view.transform = CGAffineTransformIdentity;
[pan setTranslation:CGPointZero inView:pan.view];
}];
}
if (pan.view.top > SCREEN_HEIGHT-395+130 && pan.view.top <=SCREEN_HEIGHT-395+260) {
[UIView animateWithDuration:0.1 animations:^{
pan.view.transform = CGAffineTransformMakeTranslation(0, 260);
[pan setTranslation:CGPointZero inView:pan.view];
}];
}
}
if ((pan.view.top >= SCREEN_HEIGHT-395 && pan.view.top <= SCREEN_HEIGHT-395+260) && point.y>0) {
pan.view.transform =CGAffineTransformTranslate(pan.view.transform,0, point.y);
[pan setTranslation:CGPointZero inView:pan.view];
if (pan.view.top >= SCREEN_HEIGHT-395+260) {
pan.view.transform =CGAffineTransformTranslate(pan.view.transform,0, -point.y);
[pan setTranslation:CGPointZero inView:pan.view];
}
}
if ((pan.view.top <= SCREEN_HEIGHT-395+260 && pan.view.top > SCREEN_HEIGHT-395) && point.y <0) {
pan.view.transform =CGAffineTransformTranslate(pan.view.transform,0, point.y);
[pan setTranslation:CGPointZero inView:pan.view];
if (pan.view.top <= SCREEN_HEIGHT-395) {
pan.view.transform =CGAffineTransformTranslate(pan.view.transform,0, -point.y);
[pan setTranslation:CGPointZero inView:pan.view];
}
}
// pan.view.transform =CGAffineTransformMakeTranslation(point.x, point.y);
}
@end
外面使用时:
HICDragView * view = [[HICDragView alloc] init];
view.backgroundColor = [UIColor yellowColor];
[self.view addSubview:view];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.view);
make.bottom.equalTo(self.view);
make.height.mas_equalTo(395);
}];
view.transform = CGAffineTransformMakeTranslation(0, 260);//默认向下偏移了个260
网友评论