14day-触摸事件

来源:互联网 发布:linux安装rpm包命令 编辑:程序博客网 时间:2024/05/20 03:44

前言

IOS 中的事件可分为触摸事件(multitouch events)、加速计事件( accelerometer events)、远程控制事件(remote control events)

抽屉效果的例子

一、响应者对象

在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件,我们称之为“响应者对象”--UIApplication、UIViewController、UIView都继承于UIResponder。
1、UIResponder
1>UIResponder内部提供了以下方法来处理事件

//1、触摸事件//一根或者多根手指开始触摸view,系统会自动调用view的下面方法- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;//一根或者多根手指在view上移动,系统会自动调用view的下面方法(随着手指的移动,会持续调用该方法)- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;//一根或者多根手指离开view,系统会自动调用view的下面方法- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;//触摸结束前,某个系统事件(例如电话呼入)会打断触摸过程,系统会自动调用view的下面方法[可选]- (void)touchesCancelled:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event;//2、加速计事件- (void)motionBegan:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);- (void)motionEnded:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);- (void)motionCancelled:(UIEventSubtype)motion withEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(3_0);//3、远程控制事件- (void)remoteControlReceivedWithEvent:(nullable UIEvent *)event NS_AVAILABLE_IOS(4_0);

二、UItouch

当用户用一根手指触摸屏幕时,会创建一个与手指相关联的UITouch对象;一根手指对应一个UItouch对象。
1、UITouch的作用
保持着跟手指相关的信息,比如触摸的位置、时间、阶段
1》当手指移动时,系统会更新同一个UITouch对象,使之能够一直保存该手指的触摸位置。
2》当手指离开屏幕时,系统会销毁相应的UITouch对象
3》提示:iPhone开发中,要避免使用双击事件!

2、UITouch的属性

////记录了触摸事件产生或变化时的时间,单位是秒The relative time at which the acceleration event occurred. (read-only)@property(nonatomic,readonly) NSTimeInterval      timestamp;////当前触摸事件所处的状态@property(nonatomic,readonly) UITouchPhase        phase;//// touch down within a certain point within a certain amount of timen短时间内点按屏幕的次数,可以根据tapCount判断单击、双击或更多的点击@property(nonatomic,readonly) NSUInteger          tapCount;   @property(nonatomic,readonly) UITouchType         type NS_AVAILABLE_IOS(9_0);//触摸产生时所处的窗口@property(nullable,nonatomic,readonly,strong) UIWindow                        *window;//触摸产生时所处的视图@property(nullable,nonatomic,readonly,strong) UIView                          *view;//The gesture-recognizer objects currently attached to the view.@property(nullable,nonatomic,readonly,copy)   NSArray <UIGestureRecognizer *> *gestureRecognizers

3、UITouch的方法

/*返回值表示触摸在view上的位置这里返回的位置是针对view的坐标系的(以view的左上角为原点(0, 0))调用时传入的view参数为nil的话,返回的是触摸点在UIWindow的位置*/- (CGPoint)locationInView:(nullable UIView *)view;//该方法记录了前一个触摸点的位置- (CGPoint)previousLocationInView:(nullable UIView *)view;

三、UIEvent

每产生一个事件,就会产生一个UIEvent对象,UIEvent称为事件对象,记录事件产生的时刻和类型
1、常见属性
1》事件类型

@property(nonatomic,readonly) UIEventType     type NS_AVAILABLE_IOS(3_0);@property(nonatomic,readonly) UIEventSubtype  subtype NS_AVAILABLE_IOS(3_0);

2》事件产生的时间

@property(nonatomic,readonly) NSTimeInterval  timestamp;

2、获取touch对象的方法

- (nullable NSSet <UITouch *> *)allTouches;- (nullable NSSet <UITouch *> *)touchesForWindow:(UIWindow *)window;- (nullable NSSet <UITouch *> *)touchesForView:(UIView *)view;- (nullable NSSet <UITouch *> *)touchesForGestureRecognizer:(UIGestureRecognizer *)gesture NS_AVAILABLE_IOS(3_2);// An array of auxiliary UITouch’s for the touch events that did not get delivered for a given main touch. This also includes an auxiliary version of the main touch itself.- (nullable NSArray <UITouch *> *)coalescedTouchesForTouch:(UITouch *)touch NS_AVAILABLE_IOS(9_0);// An array of auxiliary UITouch’s for touch events that are predicted to occur for a given main touch. These predictions may not exactly match the real behavior of the touch as it moves, so they should be interpreted as an estimate.- (nullable NSArray <UITouch *> *)predictedTouchesForTouch:(UITouch *)touch NS_AVAILABLE_IOS(9_0);

四、 touches和event参数

1、一次完整的触摸过程,会经历3个状态:

Ø触摸开始:- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)eventØ触摸移动:- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)eventØ触摸结束:- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)eventØ触摸取消(可能会经历):- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

4个触摸事件处理方法中,都有NSSet *touches和UIEvent *event两个参数

Ø一次完整的触摸过程中,只会产生一个事件对象,4个触摸方法都是同一个event参数Ø如果两根手指同时触摸一个view,那么view只会调用一次touchesBegan:withEvent:方法,touches参数中装着2个UITouch对象Ø如果这两根手指一前一后分开触摸同一个view,那么view会分别调用2次touchesBegan:withEvent:方法,并且每次调用时的touches参数中只包含一个UITouch对象Ø根据touchesUITouch的个数可以判断出是单点触摸还是多点触摸--UITouch的属性@property(nonatomic,readonly) NSUInteger tapCount;   可以判断多少次点击

2、 事件的产生和传递

1)发生触摸事件后,系统会将该事件加入到一个由UIApplication管理的事件队列中
2)UIApplication会从事件队列中取出最前面的事件,并将事件分发下去以便处理,通常,先发送事件给应用程序的主窗口(keyWindow)
3)主窗口会在视图层次结构中找到一个最合适的视图来处理触摸事件,但是这仅仅是整个事件处理过程的第一步
4)找到合适的视图控件后,就会调用视图控件的touches方法来作具体的事件处理

ØtouchesBegan…ØtouchesMoved…ØtouchedEnded…

这里写图片描述

p s: UIView不接收触摸事件的三种情况1).不接收用户交互userInteractionEnabled = NO 2).隐藏hidden = YES 3).透明alpha = 0.0 ~ 0.01 提示:UIImageView的userInteractionEnabled默认就是NO,因此UIImageView以及它的子控件默认是不能接收触摸事件的

3、 触摸事件处理的详细过程

1)用户点击屏幕后产生的一个触摸事件,经过一系列的传递过程后,会找到最合适的视图控件来处理这个事件2)找到最合适的视图控件后,就会调用控件的touches方法来作具体的事件处理ØtouchesBegan…ØtouchesMoved…ØtouchedEnded…3)这些touches方法的默认做法是将事件顺着响应者链条向上传递,将事件交给上一个响应者进行处理

这里写图片描述

如何判断上一个响应者
1> 如果当前这个view是控制器的view,那么控制器就是上一个响应者
2> 如果当前这个view不是控制器的view,那么父控件就是上一个响应者

4、 事件传递的完整过程

1> 先将事件对象由上往下传递(由父控件传递给子控件),找到最合适的控件来处理这个事件。

2> 调用最合适控件的touches….方法

3> 如果调用了[super touches….];就会将事件顺着响应者链条往上传递,传递给上一个响应者

4> 接着就会调用上一个响应者的touches….方法

模拟系统的hitTest方法原理

/* hitTest : withEvent: 作用:找做合适的view;当事件传递给一个控件的时候调用*/- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{    NSLog(@"%s",__func__);//    [super hitTest:point withEvent:event];//使用系统默认做法    //1、判断自己能否接受事件    if (self.userInteractionEnabled == NO || self.hidden == YES || self.alpha <= 0.01) {        //结束事件传递        return nil;    }    //2、点是否在自己身上    if (![self pointInside:point withEvent:event]) {        return nil;    }    //3、判断自己的子控件,去找有没有比自己更合适的view;--从后往前遍历自己的子控件    for (int i = self.subviews.count-1; i >= 0; i--) {        //获取子控件        UIView *childView = [self subviews][i];        //坐标系转换        CGPoint childPoint = [self convertPoint:point toView:childView];        UIView  *fitView = [childView hitTest:childPoint withEvent:event];        if (fitView) {            return fitView;        }    }    return self ;}

5、 响应者链的事件传递过程

1).如果view的控制器存在,就传递给控制器;如果控制器不存在,则将其传递给它的父视图

2).在视图层次结构的最顶级视图,如果也不能处理收到的事件或消息,则其将事件或消息传递给window对象进行处理

3).如果window对象也不处理,则其将事件或消息传递给UIApplication对象

4).如果UIApplication也不能处理该事件或消息,则将其丢弃

五、监听触摸事件的做法

方式一、如果想监听一个view上面的触摸事件,之前的做法是

Ø自定义一个view实现view的touches方法,在方法内部实现具体处理代码Ø通过touches方法监听view触摸事件,有很明显的几个缺点1)必须得自定义view2)由于是在view内部的touches方法中监听触摸事件,因此默认情况下,无法让其他外界对象监听view的触摸事件3)不容易区分用户的具体手势行为

方式二、OS 3.2之后,苹果推出了手势识别功能(Gesture Recognizer),在触摸事件处理方面,大大简化了开发者的开发难度

六、 UIGestureRecognizer

为了完成手势识别,必须借助于手势识别器—-UIGestureRecognizer。利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势

0、 手势识别的状态
这里写图片描述

typedef NS_ENUM(NSInteger, UIGestureRecognizerState) {    UIGestureRecognizerStatePossible,   // the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state没有触摸事件发生,所有手势识别的默认状态    UIGestureRecognizerStateBegan,      // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop // 一个手势已经开始但尚未改变或者完成时    UIGestureRecognizerStateChanged,    // the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop // 手势状态改变    UIGestureRecognizerStateEnded,      // the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible  // 手势完成    UIGestureRecognizerStateCancelled,  // the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible  // 手势取消,恢复至Possible状态    UIGestureRecognizerStateFailed,     // the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible   // 手势失败,恢复至Possible状态    // Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled //    UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded // the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible 识别到手势识别};

1、UIGestureRecognizer是一个抽象类,定义了所有手势的基本行为,使用它的子类才能处理具体的手势
子类:

UITapGestureRecognizer(敲击)UIPinchGestureRecognizer(捏合,用于缩放)UIPanGestureRecognizer(拖拽)UISwipeGestureRecognizer(轻扫)UIRotationGestureRecognizer(旋转)UILongPressGestureRecognizer(长按)

2、 UITapGestureRecognizer

使用手势识别器的步骤

每一个手势识别器的用法都差不多,比如UITapGestureRecognizer的使用步骤://创建手势识别器对象UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];//设置手势识别器对象的具体属性// 连续敲击2次tap.numberOfTapsRequired = 2;// 需要2根手指一起敲击tap.numberOfTouchesRequired = 2;//添加手势识别器到对应的view上[self.iconView addGestureRecognizer:tap];//监听手势的触发[tap addTarget:self action:@selector(tapIconView:)];

正文

一、抽屉效果-复杂滑动效果01
抽屉效果

#pragma mark - touches- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    //就算偏移量    UITouch *touch = [touches anyObject];    CGPoint current = [touch locationInView:self.mainView];    CGPoint pre = [touch previousLocationInView:self.mainView];    //x的偏移量    CGFloat offsetX = current.x - pre.x;    [self.mainView setFrame:[self getCurrentFrameWithOffsetX:offsetX]];}/* 根据x的便宜量计算frame */- (CGRect)getCurrentFrameWithOffsetX:(CGFloat) offsetX{    //y的偏移量    CGFloat offsetY;    if (self.mainView.frame.origin.x<0) {        offsetY = HSMaxY*offsetX/HSScreenWidth*-1;//保证正数,即保证currentHeight 小于screenHeight    }else{        offsetY = HSMaxY*offsetX/HSScreenWidth;//保证正数,即保证currentHeight 小于screenHeight    }    CGFloat currentHeight = HSScreenHeight-2*offsetY;//当前的高度    CGFloat scale =(currentHeight)/HSScreenHeight;//比例    CGRect mainFrame = self.mainView.frame;    mainFrame.origin.x += offsetX;    mainFrame.size.height *= scale;    mainFrame.size.width *= scale;    mainFrame.origin.y = (HSScreenHeight- mainFrame.size.height)*0.5;    return mainFrame;}

二、-抽屉效果-定位和复位02
定位、复位

** 抽屉效果的定位 当minx >0.5HSScreenWidth 定位到右侧 当Max <0.5HSScreenWidth 定位到左侧 */- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    //1、复位    if (self.isDraging == NO && self.mainView.frame.origin.x != 0) {        [UIView animateWithDuration:0.25 animations:^{            //复位            [self.mainView setFrame:self.view.bounds];        }];    }    //2定位    CGFloat target = 0;    if (self.mainView.frame.origin.x >0.5*HSScreenWidth) {        //定位到右侧        target = HSRightTarget;    }else if (CGRectGetMaxX(self.mainView.frame) < 0.5*HSScreenWidth ){        target = HSLeftTarget;    }    [UIView animateWithDuration:0.25 animations:^{        //复位、定位        if (target) {//不为0 才需要定位            CGFloat offsetX = target - self.mainView.frame.origin.x;            //需要定位            [self.mainView setFrame:[self getCurrentFrameWithOffsetX:offsetX]];        }else{            // 复位            [self.mainView setFrame:self.view.bounds];        }    }];    [self setIsDraging:NO];//停止拖动}

三、手势识别器
手势识别器的使用

////  ViewController.m//  20160417-手势识别////  Created by devzkn on 4/17/16.//  Copyright © 2016 hisun. All rights reserved.//#import "ViewController.h"@interface ViewController () <UIGestureRecognizerDelegate>@property (strong, nonatomic) IBOutlet UIView *ImageView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];//    [self adTap];//    [self addLongPress];//    [self addSwipe];//    [self addRotation];//    [self addPinch];    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];    [self.ImageView addGestureRecognizer:pan];}- (void)pan:(UIPanGestureRecognizer *)pan{    NSLog(@"%s",__func__);    CGPoint translation = [pan translationInView:self.ImageView];    [self.ImageView setTransform:CGAffineTransformTranslate(self.ImageView.transform, translation.x, translation.y)];//相对于上一次的位置移动    //复位    [pan setTranslation:CGPointZero inView:self.ImageView];}- (void) addPinch{    [self addRotation];    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinch:)];    [pinch setDelegate:self];    [self.ImageView addGestureRecognizer:pinch];}- (void)pinch:(UIPinchGestureRecognizer *)pinch{    [self.ImageView setTransform:CGAffineTransformScale(self.ImageView.transform, pinch.scale, pinch.scale)];    //复位    pinch.scale = 1;}- (void)addRotation{    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];    [rotation setDelegate:self];    [self.ImageView addGestureRecognizer:rotation];}#pragma mark - rolation- (void) rotation:(UIRotationGestureRecognizer *)rotation{    NSLog(@"%s",__func__);//    [self.ImageView setTransform:CGAffineTransformMakeRotation(rotation.rotation)];    [self.ImageView setTransform:CGAffineTransformRotate(self.ImageView.transform, rotation.rotation)];//基于之前的transform进行形变    rotation.rotation=0;//进行复位}- (void) addSwipe{    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];    /**     UISwipeGestureRecognizerDirectionRight = 1 << 0,     UISwipeGestureRecognizerDirectionLeft  = 1 << 1,     UISwipeGestureRecognizerDirectionUp    = 1 << 2,     UISwipeGestureRecognizerDirectionDown  = 1 << 3     */    [swipe setDirection:UISwipeGestureRecognizerDirectionLeft];    [self.ImageView addGestureRecognizer:swipe];}- (void)swipe:(UISwipeGestureRecognizer *)swipe{    NSLog(@"%s",__func__);}- (void) addLongPress{    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPress:)];    [self.ImageView addGestureRecognizer:longPress];}- (void)longPress:(UILongPressGestureRecognizer *)longPress{    if (longPress.state == UIGestureRecognizerStateEnded) {        NSLog(@"%s",__func__);    }}- (void) adTap{    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];    //    [tap setNumberOfTapsRequired:2];    //    [tap setNumberOfTouchesRequired:2];    //手势识别器的代理属性设置    [tap setDelegate:self];    [self.ImageView addGestureRecognizer:tap];}- (void)tap:(UITapGestureRecognizer *)tap {    NSLog(@"%s",__func__);}#pragma mark -  UIGestureRecognizerDelegate#pragma mark - 同时支持多个手势识别器// called when the recognition of one of gestureRecognizer or otherGestureRecognizer would be blocked by the other// return YES to allow both to recognize simultaneously. the default implementation returns NO (by default no two gestures can be recognized simultaneously)//// note: returning YES is guaranteed to allow simultaneous recognition. returning NO is not guaranteed to prevent simultaneous recognition, as the other gesture's delegate may return YES- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{    return YES;//同时支持多个手势}// called before touchesBegan:withEvent: is called on the gesture recognizer for a new touch. return NO to prevent the gesture recognizer from seeing this touch#if 0- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{    CGPoint currentFrame = [touch locationInView:self.ImageView];    if (currentFrame.x< 0.5*(CGRectGetWidth(self.ImageView.frame))) {        return NO;    }    return YES;}#endif@end

总结

HSDrawViewController.m

////  HSDrawViewController.m//  20160417-抽屉效果////  Created by devzkn on 4/17/16.//  Copyright © 2016 hisun. All rights reserved.//#import "HSDrawViewController.h"#define HSMaxY 60#define HSScreenHeight CGRectGetHeight([UIScreen mainScreen].bounds)#define HSScreenWidth CGRectGetWidth([UIScreen mainScreen].bounds)@interface HSDrawViewController ()@property (nonatomic,weak) UIView *leftView;@property (nonatomic,weak) UIView *rightView;@property (nonatomic,weak) UIView *mainView;@property (nonatomic,assign) BOOL isDraging;//是否正在拖动,用于判读是否可以复位@end@implementation HSDrawViewController- (UIView *)leftView{    if (nil == _leftView) {        UIView *tmpView = [[UIView alloc]initWithFrame:self.view.bounds];        _leftView = tmpView;        [self.view addSubview:_leftView];    }    return _leftView;}- (UIView *)rightView{    if (nil == _rightView) {        UIView *tmpView = [[UIView alloc]initWithFrame:self.view.bounds];        _rightView = tmpView;        [self.view addSubview:_rightView];    }    return _rightView;}- (UIView *)mainView{    if (nil == _mainView) {        UIView *tmpView = [[UIView alloc]initWithFrame:self.view.bounds];        _mainView = tmpView;        [self.view addSubview:_mainView];    }    return _mainView;}- (void)viewDidLoad {    [super viewDidLoad];    //1.添加子控件    [self addChildView];    //2添加观察者--监听对象属性(通知)    /*     anObserver     The object to register for KVO notifications. The observer must implement the key-value observing method observeValueForKeyPath:ofObject:change:context:.     keyPath     The key path, relative to the receiver, of the property to observe. This value must not be nil.     options     A combination of the NSKeyValueObservingOptions values that specifies what is included in observation notifications. For possible values, see NSKeyValueObservingOptions.     context     Arbitrary data that is passed to anObserver in observeValueForKeyPath:ofObject:change:context:.     */    [self.mainView addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];}- (void)addChildView{    [self.leftView setBackgroundColor:[UIColor greenColor]];    [self.rightView setBackgroundColor:[UIColor blueColor]];    [self.mainView setBackgroundColor:[UIColor redColor]];}#pragma mark - touches#define HSRightTarget 250#define HSLeftTarget -220/** 抽屉效果的定位 当minx >0.5HSScreenWidth 定位到右侧 当Max <0.5HSScreenWidth 定位到左侧 */- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    //1、复位    if (self.isDraging == NO && self.mainView.frame.origin.x != 0) {        [UIView animateWithDuration:0.25 animations:^{            //复位            [self.mainView setFrame:self.view.bounds];        }];    }    //2定位    CGFloat target = 0;    if (self.mainView.frame.origin.x >0.5*HSScreenWidth) {        //定位到右侧        target = HSRightTarget;    }else if (CGRectGetMaxX(self.mainView.frame) < 0.5*HSScreenWidth ){        target = HSLeftTarget;    }    [UIView animateWithDuration:0.25 animations:^{        //复位、定位        if (target) {//不为0 才需要定位            CGFloat offsetX = target - self.mainView.frame.origin.x;            //需要定位            [self.mainView setFrame:[self getCurrentFrameWithOffsetX:offsetX]];        }else{            // 复位            [self.mainView setFrame:self.view.bounds];        }    }];    [self setIsDraging:NO];//停止拖动}- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{    //就算偏移量    UITouch *touch = [touches anyObject];    CGPoint current = [touch locationInView:self.mainView];    CGPoint pre = [touch previousLocationInView:self.mainView];    //x的偏移量    CGFloat offsetX = current.x - pre.x;    [self.mainView setFrame:[self getCurrentFrameWithOffsetX:offsetX]];    self.isDraging = YES;}/* 根据x的便宜量计算frame */- (CGRect)getCurrentFrameWithOffsetX:(CGFloat) offsetX{    //y的偏移量    CGFloat offsetY;    if (self.mainView.frame.origin.x<0) {        offsetY = HSMaxY*offsetX/HSScreenWidth*-1;//保证正数,即保证currentHeight 小于screenHeight    }else{        offsetY = HSMaxY*offsetX/HSScreenWidth;//保证正数,即保证currentHeight 小于screenHeight    }    CGFloat currentHeight = HSScreenHeight-2*offsetY;//当前的高度    CGFloat scale =(currentHeight)/HSScreenHeight;//比例    CGRect mainFrame = self.mainView.frame;    mainFrame.origin.x += offsetX;    mainFrame.size.height *= scale;    mainFrame.size.width *= scale;    mainFrame.origin.y = (HSScreenHeight- mainFrame.size.height)*0.5;    return mainFrame;}#pragma  mark -  The observer must implement the key-value observing method observeValueForKeyPath:ofObject:change:context:./* keyPath: The key path, relative to object, to the value that has changed. object: The source object of the key path keyPath. change: A dictionary that describes the changes that have been made to the value of the property at the key path keyPath relative to object. Entries are described in Change Dictionary Keys. context: The value that was provided when the receiver was registered to receive key-value observation notifications. */- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{    if (self.mainView.frame.origin.x < 0) {        [self.leftView setHidden:YES];//显示右边视图        [self.rightView setHidden:NO];    }else if (self.mainView.frame.origin.x >0){//往右移动        [self.rightView setHidden:YES];        [self.leftView setHidden:NO];    }}@end
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 猫不小心摔一下怎么办 一氧化二氮中毒怎么办 电脑开机变慢了怎么办 怎么办抚顺韦德健身卡 预售健身卡合法吗怎么办 被浓硫酸泼到怎么办 婴儿误喝了生水怎么办 宝宝喝了生水拉肚子怎么办 因妈妈喝生水宝宝拉肚子怎么办 喝了几口生水怎么办 不小心吃到蟑螂怎么办 吃了有蛆的樱桃怎么办 不小心误食了蛆怎么办 吃了有蟑螂的汤怎么办 调节天平时指针向右怎么办 香薰蜡烛融化了怎么办 香薰蜡烛挂壁怎么办y 粗蜡烛只烧中间怎么办 紫薯馒头变绿怎么办 小孩手开水烫了怎么办 被油烫伤了怎么办才不留疤 烫伤水泡蹭破了怎么办 烧伤的水泡破了怎么办 烧伤后水泡破了怎么办 烫伤泡破了红肿怎么办 烧伤第二天水泡破了怎么办? 烧伤后换药特别疼怎么办 盐酸溅到皮肤上怎么办 磷性磷酸酶高440怎么办 浓硫酸沾到皮肤上怎么办 浓硫酸溅到皮肤上怎么办 浓硫酸滴到皮肤上怎么办 浓硫酸洒在皮肤上怎么办 浓硫酸溅到眼睛里怎么办 盐酸弄到眼睛了怎么办 稀硫酸进眼睛里怎么办 草酸弄到皮肤上怎么办 大理石被盐酸烧发白怎么办 香薰蜡烛化了怎么办 吸入了大量燃烧纸气体怎么办 狗链条上锈了怎么办