国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

抽屜效果實現(xiàn)

2019-11-09 17:41:34
字體:
供稿:網(wǎng)友
#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];         // 監(jiān)聽主視圖的frame的變化    [self.mainView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];}// 屬性變化,會調(diào)用此方法- (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;         // 設(shè)置當(dāng)前視圖的frame    self.mainView.frame = [self frameWithOffsetX:offsetX];         // 記錄下當(dāng)前正在拖拽    self.isDraging = YES;}// 根據(jù)X的偏移量計算出當(dāng)前視圖的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;    }         // 計算當(dāng)前視圖的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;    // 獲取當(dāng)前主視圖的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;}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 栾城县| 德化县| 资兴市| 博野县| 科尔| 白山市| 襄樊市| 卢龙县| 威远县| 唐海县| 潼关县| 施秉县| 徐闻县| 荣昌县| 秭归县| 弋阳县| 台南市| 蓝山县| 剑河县| 永和县| 自贡市| 广灵县| 灵川县| 普洱| 建瓯市| 襄城县| 马尔康县| 辽源市| 金溪县| 望城县| 灌阳县| 寿宁县| 津市市| 会同县| 平邑县| 鹤岗市| 丽江市| 阿勒泰市| 垫江县| 凯里市| 宕昌县|