本文实例介绍了iOS实现左右拖动抽屉效果,具体内容如下
利用了触摸事件滑动 touchesMoved: 来触发左右视图的出现和消失 利用loadView方法中添加view 在self.view载入前就把 左右中View都设置好frame 每一个方法都由单独的功能。
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
#import "DarwViewController.h"
@interface DarwViewController ()
@property (nonatomic, weak) UIView *leftView;
@property (nonatomic, weak) UIView *rightView;
@property (nonatomic, weak) UIView *mainView;
/**
* 动画是否进行
*/
@property (nonatomic ,assign) BOOL animating;
@end
@implementation DarwViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)loadView
{
self.view = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
//左边view
UIView *leftView = [[UIView alloc]initWithFrame:self.view.frame];
[self.view addSubview:leftView];
leftView.backgroundColor= [UIColor redColor];
self.leftView = leftView;
//右边View
UIView *rightView = [[UIView alloc]initWithFrame:self.view.frame];
[self.view addSubview:rightView];
rightView.backgroundColor= [UIColor blueColor];
self.rightView = rightView;
//主页面
UIView *mainView = [[UIView alloc]initWithFrame:self.view.frame];
[self.view addSubview:mainView];
mainView.backgroundColor= [UIColor yellowColor];
self.mainView = mainView;
//KVO监听
[self.mainView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
}
/**
* KVO回调方法 当mainView Frame值改变时触发
*
* @param keyPath <#keyPath description#>
* @param object <#object description#>
* @param change <#change description#>
* @param context <#context description#>
*/
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
if (self.animating) return; //如果mainView正在动画 就不执行
if (self.mainView.frame.origin.x > 0 )
{
//X > 0 就隐藏右边View 显示左边View
self.rightView.hidden = YES;
self.leftView.hidden = NO;
}
else if (self.mainView.frame.origin.x < 0)
{
//X < 0 就隐藏左边View 显示右边VIew
self.leftView.hidden = YES;
self.rightView.hidden = NO;
}
}
#pragma mark -- 触摸事件
-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//获得触摸对象
UITouch *touch = [touches anyObject];
//获得当前触摸点
CGPoint currentPoint = [touch locationInView:self.view];
//获得上一个触摸点
CGPoint previousPoint = [touch previousLocationInView:self.view];
//计算x方向的偏移量
CGFloat offsetX = currentPoint.x - previousPoint.x;
// 根据x的偏移量计算y的偏移量
self.mainView.frame = [self rectWithOffsetX:offsetX];
}
#define screenW [UIScreen mainScreen].bounds.size.width
#define screenH [UIScreen mainScreen].bounds.size.height
/**
* 计算主视图的frame
*
* @param offsetX x的偏移量
*
* @return 偏移后新的frame
*/
- (CGRect ) rectWithOffsetX:(CGFloat )offsetX
{
//Y轴的偏移量
CGFloat offsetY = (screenH *1/5) * (offsetX/screenW);
//比例 :(用于宽高的缩放)
CGFloat scale = (screenH - offsetY *2) / screenH;
if (self.mainView.frame.origin.x < 0 )
{
//如果x是负数 及左边View要显示
//比例就要设为比1小
scale = 2 - scale;
}
//获取当前mainView的frame
CGRect frame = self.mainView.frame;
//重新设置mainView的frame值
frame.size.width = frame.size.width *scale >screenW ? screenW : frame.size.width *scale;
frame.size.height = frame.size.height *scale >screenH ? screenH :frame.size.height *scale;
frame.origin.x += offsetX;
frame.origin.y =(screenH - frame.size.height)*0.5;
//返回偏移后新的frame
return frame;
}
#define maxRightX (screenW *0.8)
#define maxLeftX (-screenW *0.6)
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
CGFloat targetX = 0;
//如果松手的那一下 当前mainVIew的x大于屏幕的一半
if (self.mainView.frame.origin.x > screenW * 0.5)
{
//向右边定位
targetX = maxRightX;
}
//如果松手的那一下 当前mainVIew的最大X值小于屏幕的一半
else if (CGRectGetMaxX(self.mainView.frame) < screenW *0.5)
{
//向左边定位
targetX = maxLeftX;
}
//计算偏移量
CGFloat offsetX = targetX -self.mainView.frame.origin.x;
self.animating = YES;
[UIView animateWithDuration:0.4 animations:^{
if (targetX == 0)
{
//如果targetX==0 复位
self.mainView.frame = self.view.frame;
}
else
{
//如果targetX != 0 那就到指定位置
self.mainView.frame = [self rectWithOffsetX:offsetX];
}
} completion:^(BOOL finished) {
self.animating = NO;
}];
}
@end
|
以上就是本文的全部内容,希望对大家的学习有所帮助。








发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。