最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS實(shí)現(xiàn)左右拖動(dòng)抽屜效果

 更新時(shí)間:2016年02月27日 09:53:14   作者:wAikAp  
這篇文章主要介紹了iOS實(shí)現(xiàn)左右拖動(dòng)抽屜效果,理解ios平臺(tái)類似于QQ主頁面,利用觸摸事件滑動(dòng)touchesMoved實(shí)現(xiàn)的效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例介紹了iOS實(shí)現(xiàn)左右拖動(dòng)抽屜效果,具體內(nèi)容如下

利用了觸摸事件滑動(dòng) touchesMoved: 來觸發(fā)左右視圖的出現(xiàn)和消失 利用loadView方法中添加view 在self.view載入前就把 左右中View都設(shè)置好frame 每一個(gè)方法都由單獨(dú)的功能。

#import "DarwViewController.h"
@interface DarwViewController ()
@property (nonatomic, weak) UIView *leftView;
@property (nonatomic, weak) UIView *rightView;
@property (nonatomic, weak) UIView *mainView;
/**
 * 動(dòng)畫是否進(jìn)行
 */
@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監(jiān)聽
 [self.mainView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
}
/**
 * KVO回調(diào)方法 當(dāng)mainView Frame值改變時(shí)觸發(fā)
 *
 * @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正在動(dòng)畫 就不執(zhí)行
 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
{
 //獲得觸摸對(duì)象
 UITouch *touch = [touches anyObject];
  
 //獲得當(dāng)前觸摸點(diǎn)
 CGPoint currentPoint = [touch locationInView:self.view];
 //獲得上一個(gè)觸摸點(diǎn)
 CGPoint previousPoint = [touch previousLocationInView:self.view];
  
 //計(jì)算x方向的偏移量
 CGFloat offsetX = currentPoint.x - previousPoint.x;
// 根據(jù)x的偏移量計(jì)算y的偏移量
 self.mainView.frame = [self rectWithOffsetX:offsetX];
  
}
#define screenW [UIScreen mainScreen].bounds.size.width
#define screenH [UIScreen mainScreen].bounds.size.height
/**
 * 計(jì)算主視圖的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是負(fù)數(shù) 及左邊View要顯示
  //比例就要設(shè)為比1小
  scale = 2 - scale;
 }
 //獲取當(dāng)前mainView的frame
 CGRect frame = self.mainView.frame;
  
 //重新設(shè)置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;
 //如果松手的那一下 當(dāng)前mainVIew的x大于屏幕的一半
 if (self.mainView.frame.origin.x > screenW * 0.5)
 {
  //向右邊定位
  targetX = maxRightX;
 }
 //如果松手的那一下 當(dāng)前mainVIew的最大X值小于屏幕的一半
 else if (CGRectGetMaxX(self.mainView.frame) < screenW *0.5)
 {
  //向左邊定位
  targetX = maxLeftX;
 }
  
 //計(jì)算偏移量
 CGFloat offsetX = targetX -self.mainView.frame.origin.x;
  
 self.animating = YES;
  
 [UIView animateWithDuration:0.4 animations:^{
  if (targetX == 0)
  {
   //如果targetX==0 復(fù)位
   self.mainView.frame = self.view.frame;
  }
  else
  {
   //如果targetX != 0 那就到指定位置
   self.mainView.frame = [self rectWithOffsetX:offsetX];
  }
 } completion:^(BOOL finished) {
  self.animating = NO;
 }];
   
}
@end

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論

东海县| 宣化县| 榆社县| 乐都县| 盱眙县| 饶平县| 伊宁县| 广河县| 故城县| 都匀市| 元氏县| 湾仔区| 玛纳斯县| 西峡县| 通江县| 日喀则市| 镇康县| 蓬溪县| 建平县| 渭源县| 思南县| 通州区| 奉化市| 南投市| 徐汇区| 屏东县| 灯塔市| 宁德市| 攀枝花市| 永修县| 剑川县| 铁岭县| 金寨县| 华阴市| 中西区| 潞城市| 余姚市| 剑河县| 孝感市| 鄂托克旗| 闽侯县|