亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS簡單抽屜效果的實現(xiàn)方法

 更新時間:2022年08月08日 11:08:37   作者:綠葉清風  
這篇文章主要為大家詳細介紹了iOS簡單抽屜效果的實現(xiàn)方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了iOS實現(xiàn)簡單抽屜效果的具體代碼,供大家參考,具體內容如下

實現(xiàn)思路及步驟:

1、首先準備要滑動的view

#warning 第一步
- (void)addChildView
{
? ? // left
? ? UIView *leftView = [[UIView alloc] initWithFrame:self.view.bounds];
? ? leftView.backgroundColor = [UIColor greenColor];
? ? [self.view addSubview:leftView];
? ? _leftView = leftView;
? ??
? ? // right
? ? UIView *rightView = [[UIView alloc] initWithFrame:self.view.bounds];
? ? rightView.backgroundColor = [UIColor blueColor];
? ? [self.view addSubview:rightView];
? ? _rightView = rightView;
? ??
? ? // mainView
? ? UIView *mainView = [[UIView alloc] initWithFrame:self.view.bounds];
? ? mainView.backgroundColor = [UIColor redColor];
? ? [self.view addSubview:mainView];
? ? _mainView = mainView;
}

2、監(jiān)聽觸摸事件,記錄橫軸方向的偏移量,有了偏移量就可以通過偏移量改動視圖的位置

#warning 第二布
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
? ? // 獲取UITouch對象
? ? UITouch *touch = [touches anyObject];
? ??
? ? // 獲取當前點
? ? CGPoint currentPoint = [touch locationInView:self.view];
? ??
? ? // 獲取上一個點
? ? CGPoint prePoint = [touch previousLocationInView:self.view];
? ??
? ? // x軸偏移量:當手指移動一點的時候,x偏移多少
? ? CGFloat offsetX = currentPoint.x - prePoint.x;
? ??
? ? // 設置當前主視圖的frame
? ? _mainView.frame = [self getCurrentFrameWithOffsetX:offsetX];
? ??
? ??
? ? _isDraging = YES;
}
? 如何實時監(jiān)聽一個對象的屬性(只能監(jiān)聽對象,不能監(jiān)聽結構體)?kvo
? // 1.監(jiān)聽 ?2 ,實現(xiàn):observeValueForKeyPath 方法
? /**
? ? ?* ?給_mainView添加一個觀察者
? ? ?*
? ? ?* ?KeyPath:監(jiān)聽frame這個屬性
? ? ?*
? ? ?* ?options:監(jiān)聽新值的改變
? ? ?*/
? ? [_mainView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil]; //實時監(jiān)聽mainview的frame
?
?
// 當_mainView的frame屬性改變的時候就會調用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
? ? NSLog(@"%@", NSStringFromCGRect(_mainView.frame));//當前的frame
? ??
? ? if (_mainView.frame.origin.x < 0) { // 主界面的x<0,往左移動
? ? ? ? // 顯示右邊
? ? ? ? _rightView.hidden = NO;
? ? ? ? // 隱藏左邊
? ? ? ? _leftView.hidden = YES;
? ? }else if (_mainView.frame.origin.x > 0){ // 往右移動
? ? ? ? // 顯示左邊
? ? ? ? _rightView.hidden = YES;
? ? ? ? // 隱藏右邊
? ? ? ? _leftView.hidden = NO;
? ? ? ??
? ? }
}?

當x軸偏移的時候,如何縮放高度?
當x偏移時,y軸縮小為: x的偏移量/

#define HMMaxY 60
// 當手指偏移一點,根據(jù)X軸的偏移量算出當前主視圖的frame
- (CGRect)getCurrentFrameWithOffsetX:(CGFloat)offsetX
{
? ? CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
? ? CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
? ??
? ? // 獲取y軸偏移量,手指每移動一點,y軸偏移多少
? ? CGFloat offsetY = offsetX * HMMaxY / screenW;//HMMaxY 縮放的最大Y?
? ??
? ? CGFloat scale = (screenH - 2 * offsetY) / screenH;//比例?
? ??
? ? if (_mainView.frame.origin.x < 0) { // 往左邊滑動
? ? ? ? scale = (screenH + 2 * offsetY) / screenH;
? ? }
? ??
? ? // 獲取之前的frame
? ? CGRect frame = _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 HMRTarget 250
#define HMLTarget -220
/*
?_mainView.frame.origin.x > screenW * 0.5 定位到右邊
? CGRectGetMaxX(_mainView.frame) < screenW * 0.5 定位到左邊 -220
?
?*/
// 定位
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
? ??
? ? // 復位
? ? if (_isDraging == NO && _mainView.frame.origin.x != 0) {
? ? ? ? [UIView animateWithDuration:0.25 animations:^{
? ? ? ? ? ??
? ? ? ? ? ? _mainView.frame = self.view.bounds;
? ? ? ? }];
? ? }
? ??
? ??
? ? CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
? ??
? ? CGFloat target = 0;
? ? if (_mainView.frame.origin.x > screenW * 0.5) { // 定位到右邊
? ? ? ? target = HMRTarget;
? ? }else if (CGRectGetMaxX(_mainView.frame) < screenW * 0.5) { // 定位到左邊
? ? ? ? target = HMLTarget;
? ? }
? ??
? ? [UIView animateWithDuration:0.25 animations:^{
? ? ? ??
? ? ? ? if (target) { // 在需要定位左邊或者右邊
? ? ? ? ? ??
? ? ? ? ? ? // 獲取x軸偏移量
? ? ? ? ? ? CGFloat offsetX = target - _mainView.frame.origin.x;
? ? ? ? ? ??
? ? ? ? ? ? // 設置當前主視圖的frame
? ? ? ? ? ? _mainView.frame = [self getCurrentFrameWithOffsetX:offsetX];
? ? ? ? ? ??
? ? ? ? }else{ // 還原
? ? ? ? ? ? _mainView.frame = self.view.bounds;
? ? ? ? }
? ? }];
? ??
? ? _isDraging = NO;
? ??
}

全:

@interface HMDrawViewController ()
?
?
@property (nonatomic, assign) BOOL isDraging;
@end
?
@implementation HMDrawViewController
?
?
- (void)viewDidLoad
{
? ? // UIViewController
? ? [super viewDidLoad];
? ? // Do any additional setup after loading the view.
? ?
? ? // 1.添加子控件
? ? [self addChildView];
#warning 第三步 觀察_mainView的frame改變
? ? // 2.監(jiān)聽
? ? /**
? ? ?* ?給_mainView添加一個觀察者
? ? ?*
? ? ?* ?KeyPath:監(jiān)聽frame這個屬性
? ? ?*
? ? ?* ?options:監(jiān)聽新值的改變
? ? ?*/
? ? [_mainView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
? ?
}
?
// 當_mainView的frame屬性改變的時候就會調用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
? ? NSLog(@"%@", NSStringFromCGRect(_mainView.frame));
? ??
? ? if (_mainView.frame.origin.x < 0) { // 往左移動
? ? ? ? // 顯示右邊
? ? ? ? _rightView.hidden = NO;
? ? ? ? // 隱藏左邊
? ? ? ? _leftView.hidden = YES;
? ? }else if (_mainView.frame.origin.x > 0){ // 往右移動
? ? ? ? // 顯示左邊
? ? ? ? _rightView.hidden = YES;
? ? ? ? // 隱藏右邊
? ? ? ? _leftView.hidden = NO;
? ? ? ??
? ? }
}
?
#warning 第一步
- (void)addChildView
{
? ? // left
? ? UIView *leftView = [[UIView alloc] initWithFrame:self.view.bounds];
? ? leftView.backgroundColor = [UIColor greenColor];
? ? [self.view addSubview:leftView];
? ? _leftView = leftView;
? ??
? ? // right
? ? UIView *rightView = [[UIView alloc] initWithFrame:self.view.bounds];
? ? rightView.backgroundColor = [UIColor blueColor];
? ? [self.view addSubview:rightView];
? ? _rightView = rightView;
? ??
? ? // mainView
? ? UIView *mainView = [[UIView alloc] initWithFrame:self.view.bounds];
? ? mainView.backgroundColor = [UIColor redColor];
? ? [self.view addSubview:mainView];
? ? _mainView = mainView;
}
?
#warning 第二布
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
? ? // 獲取UITouch對象
? ? UITouch *touch = [touches anyObject];
? ??
? ? // 獲取當前點
? ? CGPoint currentPoint = [touch locationInView:self.view];
? ??
? ? // 獲取上一個點
? ? CGPoint prePoint = [touch previousLocationInView:self.view];
? ??
? ? // x軸偏移量:當手指移動一點的時候,x偏移多少
? ? CGFloat offsetX = currentPoint.x - prePoint.x;
? ??
? ? // 設置當前主視圖的frame
? ? _mainView.frame = [self getCurrentFrameWithOffsetX:offsetX];
? ??
? ??
? ? _isDraging = YES;
}
#warning 第四步
#define HMMaxY 60
// 當手指偏移一點,根據(jù)X軸的偏移量算出當前主視圖的frame
- (CGRect)getCurrentFrameWithOffsetX:(CGFloat)offsetX
{
? ? CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
? ? CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
? ??
? ? // 獲取y軸偏移量,手指每移動一點,y軸偏移多少
? ? CGFloat offsetY = offsetX * HMMaxY / screenW;
? ??
? ? CGFloat scale = (screenH - 2 * offsetY) / screenH;
? ??
? ? if (_mainView.frame.origin.x < 0) { // 往左邊滑動
? ? ? ? scale = (screenH + 2 * offsetY) / screenH;
? ? }
? ??
? ? // 獲取之前的frame
? ? CGRect frame = _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 HMRTarget 250
#define HMLTarget -220
/*
?_mainView.frame.origin.x > screenW * 0.5 定位到右邊
? CGRectGetMaxX(_mainView.frame) < screenW * 0.5 定位到左邊 -220
?
?*/
// 定位
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
? ??
? ? // 復位
? ? if (_isDraging == NO && _mainView.frame.origin.x != 0) {
? ? ? ? [UIView animateWithDuration:0.25 animations:^{
? ? ? ? ? ??
? ? ? ? ? ? _mainView.frame = self.view.bounds;
? ? ? ? }];
? ? }
? ??
? ??
? ? CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
? ??
? ? CGFloat target = 0;
? ? if (_mainView.frame.origin.x > screenW * 0.5) { // 定位到右邊
? ? ? ? target = HMRTarget;
? ? }else if (CGRectGetMaxX(_mainView.frame) < screenW * 0.5) { // 定位到左邊
? ? ? ? target = HMLTarget;
? ? }
? ??
? ? [UIView animateWithDuration:0.25 animations:^{
? ? ? ??
? ? ? ? if (target) { // 在需要定位左邊或者右邊
? ? ? ? ? ??
? ? ? ? ? ? // 獲取x軸偏移量
? ? ? ? ? ? CGFloat offsetX = target - _mainView.frame.origin.x;
? ? ? ? ? ??
? ? ? ? ? ? // 設置當前主視圖的frame
? ? ? ? ? ? _mainView.frame = [self getCurrentFrameWithOffsetX:offsetX];
? ? ? ? ? ??
? ? ? ? }else{ // 還原
? ? ? ? ? ? _mainView.frame = self.view.bounds;
? ? ? ? }
? ? }];
? ??
? ? _isDraging = NO;
? ??
}

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

相關文章

  • iOS - UIButton(UIEdgeInsets)/設置button上的文字和圖片上下垂直居中對齊

    iOS - UIButton(UIEdgeInsets)/設置button上的文字和圖片上下垂直居中對齊

    這篇文章主要介紹了iOS - UIButton(UIEdgeInsets)/設置button上的文字和圖片上下垂直居中對齊的相關資料,需要的朋友可以參考下
    2015-09-09
  • iOS創(chuàng)建與使用靜態(tài)庫

    iOS創(chuàng)建與使用靜態(tài)庫

    這篇文章主要為大家詳細介紹了iOS創(chuàng)建與使用靜態(tài)庫的相關資料,感興趣的小伙伴們可以參考一下
    2016-03-03
  • iOS中NSThread使用示例詳解

    iOS中NSThread使用示例詳解

    這篇文章主要為大家介紹了iOS中NSThread使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • iOS獲取當前網(wǎng)絡環(huán)境的實現(xiàn)方法(推薦)

    iOS獲取當前網(wǎng)絡環(huán)境的實現(xiàn)方法(推薦)

    下面小編就為大家?guī)硪黄猧OS獲取當前網(wǎng)絡環(huán)境的實現(xiàn)方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • iOS開發(fā)數(shù)獨小游戲實例

    iOS開發(fā)數(shù)獨小游戲實例

    這篇文章主要為大家詳細介紹了iOS開發(fā)數(shù)獨小游戲實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • iOS復數(shù)cell下優(yōu)雅的代碼結構詳解

    iOS復數(shù)cell下優(yōu)雅的代碼結構詳解

    這篇文章主要給大家介紹了關于iOS復數(shù)cell下優(yōu)雅的代碼結構的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用iOS具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-04-04
  • iOS 請求權限封裝類的實例代碼

    iOS 請求權限封裝類的實例代碼

    下面小編就為大家分享一篇iOS 請求權限封裝類的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • iOS應用開發(fā)中使UITextField實現(xiàn)placeholder屬性的方法

    iOS應用開發(fā)中使UITextField實現(xiàn)placeholder屬性的方法

    這篇文章主要介紹了iOS應用開發(fā)中使UITextField實現(xiàn)placeholder屬性的方法,示例代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下
    2016-04-04
  • iOS消息發(fā)送和轉發(fā)示例詳解

    iOS消息發(fā)送和轉發(fā)示例詳解

    這篇文章主要給大家介紹了關于iOS消息發(fā)送和轉發(fā)的相關資料,用Objective-C的術語來講,這叫做“給某個對象發(fā)送某條消息”。文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2018-03-03
  • iOS開發(fā)學習TableView展現(xiàn)一個list實例

    iOS開發(fā)學習TableView展現(xiàn)一個list實例

    這篇文章主要為大家介紹了iOS系列學習TableView展現(xiàn)一個list實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11

最新評論