你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS完成閣下拖動抽屜後果

iOS完成閣下拖動抽屜後果

編輯:IOS開發綜合

本文實例引見了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

以上就是本文的全體內容,願望對年夜家的進修有所贊助。

【iOS完成閣下拖動抽屜後果】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved