首页 > 学院 > 开发设计 > 正文

抽屉效果实现

2019-11-09 17:41:34
字体:
来源:转载
供稿:网友
#import "ViewController.h"#define maxY 60@interface ViewController ()@PRoperty (nonatomic, weak) UIView *mainView;@property (nonatomic, weak) UIView *leftView;@property (nonatomic, weak) UIView *rightView;@property (nonatomic, assign) BOOL isDraging;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // 添加所有子控件    [self addAllChildView];         // 监听主视图的frame的变化    [self.mainView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];}// 属性变化,会调用此方法- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {         if (self.mainView.frame.origin.x < 0) {        self.rightView.hidden = NO;        self.leftView.hidden = YES;    }else if(self.mainView.frame.origin.x > 0){        self.rightView.hidden = YES;        self.leftView.hidden = NO;    }}- (void)addAllChildView {         // leftView    UIView *leftView = [[UIView alloc] initWithFrame:self.view.bounds];    leftView.backgroundColor = [UIColor greenColor];    leftView.alpha = 0.3;    [self.view addSubview:leftView];    self.leftView = leftView;         // rightView    UIView *rightView = [[UIView alloc] initWithFrame:self.view.bounds];    rightView.backgroundColor = [UIColor redColor];    rightView.alpha = 0.3;    [self.view addSubview:rightView];         // mainView    UIView *mainView = [[UIView alloc] initWithFrame:self.view.bounds];    mainView.backgroundColor = [UIColor blueColor];    mainView.alpha = 0.3;    [self.view addSubview:mainView];    self.mainView = mainView;}// 添加拖拽方法- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {         // 获取手指    UITouch *touch = [touches anyObject];         CGPoint curP = [touch locationInView:self.view];         CGPoint preP = [touch previousLocationInView:self.view];         // 获取手指每移动一下, X轴的偏移量    CGFloat offsetX = curP.x - preP.x;         // 设置当前视图的frame    self.mainView.frame = [self frameWithOffsetX:offsetX];         // 记录下当前正在拖拽    self.isDraging = YES;}// 根据X的偏移量计算出当前视图的frame- (CGRect)frameWithOffsetX:(CGFloat)offsetX {    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;    CGFloat screenH = [UIScreen mainScreen].bounds.size.height;         // 获取手指每偏移一点, Y值需要偏移多少    CGFloat offsetY = offsetX * maxY / screenW;         // 获取缩放比例    CGFloat scale = (screenH - 2 * offsetY) / screenH;         if (self.mainView.frame.origin.x < 0) {        scale = (screenH + 2 * offsetY) / screenH;    }         // 计算当前视图的frame    CGRect frame = self.mainView.frame;    frame.origin.x += offsetX;    frame.size.height = frame.size.height * scale;    frame.size.width = frame.size.width * scale;    frame.origin.y = (screenH - frame.size.height) * 0.5;         return frame;}#define targetRX 300#define targetLX -250- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {         if (self.isDraging == NO && self.mainView.frame.origin.x != 0) {        [UIView animateWithDuration:0.25 animations:^{            self.mainView.frame = self.view.bounds;        }];        return;    }         // 定位功能    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;    // 获取当前主视图的frame    CGRect frame = self.mainView.frame;         CGFloat target = 0;    if (frame.origin.x > screenW * 0.5) { //        target = targetRX;    }else if(CGRectGetMaxX(self.mainView.frame) < screenW * 0.5){        // 定位到左边        target = targetLX;    }                  if(target == 0) {        [UIView animateWithDuration:0.25 animations:^{            self.mainView.frame = self.view.bounds;        }];    }else {        [UIView animateWithDuration:0.25 animations:^{            // 获取X轴的偏移量            CGFloat offsetX = target - frame.origin.x;            self.mainView.frame = [self frameWithOffsetX:offsetX];        }];    }         // 没有拖拽    self.isDraging = NO;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表