iOS手势操作简介(五)

利用手势操作实现抽屉效果:

第一步:搭建UI

(void)addChildView
{
// left
UIView *leftView = [[UIView alloc] initWithFrame:self.view.bounds];
leftView.backgroundColor = [UIColor greenColor];
[self.view addSubview:leftView];
_leftView = leftView;

// right
UIView *rightView = [[UIView alloc] initWithFrame:self.view.bounds];
rightView.backgroundColor = [UIColor blueColor];
[self.view addSubview:rightView];
_rightView = rightView;

// mainView
UIView *mainView = [[UIView alloc] initWithFrame:self.view.bounds];
mainView.backgroundColor = [UIColor redColor];
[self.view addSubview:mainView];
_mainView = mainView;

}

第二步:获取手势操作对象,获取手势移动大小
- (void)touchesMoved:(NSSet )touches withEvent:(UIEvent )event
{
// 获取UITouch对象
UITouch *touch = [touches anyObject];

// 获取当前点
CGPoint currentPoint = [touch locationInView:self.view];

// 获取上一个点
CGPoint prePoint = [touch previousLocationInView:self.view];

// x轴偏移量:当手指移动一点的时候,x偏移多少
CGFloat offsetX = currentPoint.x - prePoint.x;

// 设置当前主视图的frame
_mainView.frame = [self getCurrentFrameWithOffsetX:offsetX];


_isDraging = YES;

}
第三步:利用手势平移量来重写计算新的frame
//#define HMMaxY 60
// 当手指偏移一点,根据X轴的偏移量算出当前主视图的frame
- (CGRect)getCurrentFrameWithOffsetX:(CGFloat)offsetX
{
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
CGFloat screenH = [UIScreen mainScreen].bounds.size.height;

// 获取y轴偏移量,手指每移动一点,y轴偏移多少
CGFloat offsetY = offsetX * HMMaxY / screenW;

CGFloat scale = (screenH - 2 * offsetY) / screenH;

if (_mainView.frame.origin.x < 0) { // 往左边滑动
    scale = (screenH + 2 * offsetY) / screenH;
}

// 获取之前的frame
CGRect frame = _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 HMRTarget 250
//#define HMLTarget -220
/*
_mainView.frame.origin.x > screenW * 0.5 定位到右边
CGRectGetMaxX(_mainView.frame) < screenW * 0.5 定位到左边 -220

*/
// 定位
- (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
{

// 复位
if (_isDraging == NO && _mainView.frame.origin.x != 0) {
    [UIView animateWithDuration:0.25 animations:^{

        _mainView.frame = self.view.bounds;
    }];
}


CGFloat screenW = [UIScreen mainScreen].bounds.size.width;

CGFloat target = 0;
if (_mainView.frame.origin.x > screenW * 0.5) { // 定位到右边
    target = HMRTarget;
}else if (CGRectGetMaxX(_mainView.frame) < screenW * 0.5) { // 定位到左边
    target = HMLTarget;
}

[UIView animateWithDuration:0.25 animations:^{

    if (target) { // 在需要定位左边或者右边

        // 获取x轴偏移量
        CGFloat offsetX = target - _mainView.frame.origin.x;

        // 设置当前主视图的frame
        _mainView.frame = [self getCurrentFrameWithOffsetX:offsetX];

    }else{ // 还原
        _mainView.frame = self.view.bounds;
    }
}];

_isDraging = NO;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值