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

首頁 > 系統 > iOS > 正文

iOS實現左右拖動抽屜效果

2020-07-26 03:27:07
字體:
來源:轉載
供稿:網友

本文實例介紹了iOS實現左右拖動抽屜效果,具體內容如下

利用了觸摸事件滑動 touchesMoved: 來觸發左右視圖的出現和消失 利用loadView方法中添加view 在self.view載入前就把 左右中View都設置好frame 每一個方法都由單獨的功能。

#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

以上就是本文的全部內容,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 佛冈县| 奈曼旗| 威宁| 上蔡县| 阿鲁科尔沁旗| 台湾省| 武宁县| 建阳市| 克拉玛依市| 绵竹市| 扶风县| 丰台区| 浙江省| 海安县| 鸡西市| 沈阳市| 道孚县| 安庆市| 图木舒克市| 侯马市| 手游| 洱源县| 定州市| 平罗县| 怀远县| 定陶县| 汶上县| 防城港市| 怀远县| 昌吉市| 深圳市| 米脂县| 平顺县| 托克逊县| 卓资县| 武安市| 外汇| 乳山市| 二手房| 曲靖市| 星座|