#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];
[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 {
UIView *leftView = [[UIView alloc] initWithFrame:self.view.bounds];
leftView.backgroundColor = [UIColor greenColor];
leftView.alpha = 0.3;
[self.view addSubview:leftView];
self.leftView = leftView;
UIView *rightView = [[UIView alloc] initWithFrame:self.view.bounds];
rightView.backgroundColor = [UIColor redColor];
rightView.alpha = 0.3;
[self.view addSubview:rightView];
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];
CGFloat offsetX = curP.x - preP.x;
self.mainView.frame = [self frameWithOffsetX:offsetX];
self.isDraging = YES;
}
- (CGRect)frameWithOffsetX:(CGFloat)offsetX {
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
CGFloat offsetY = offsetX * maxY / screenW;
CGFloat scale = (screenH - 2 * offsetY) / screenH;
if
(self.mainView.frame.origin.x < 0) {
scale = (screenH + 2 * offsetY) / screenH;
}
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;
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:^{
CGFloat offsetX = target - frame.origin.x;
self.mainView.frame = [self frameWithOffsetX:offsetX];
}];
}
self.isDraging = NO;
}