IOS第15天(2,事件处理,侧滑菜单,抽屉效果)

******HMDrawViewController.m

#import "HMDrawViewController.h"

@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.监听
    /**
     *  给_mainView添加一个观察者
     *
     *  KeyPath:监听frame这个属性
     *
     *  options:监听新值的改变
     */
    [_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
// 当手指偏移一点,根据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;

}

@end

***HMDrawViewController.h文件

#import <UIKit/UIKit.h>

@interface HMDrawViewController : UIViewController

@property (nonatomic, weak, readonly) UIView *mainView;
@property (nonatomic, weak, readonly) UIView *leftView;
@property (nonatomic, weak, readonly) UIView *rightView;

@end
时间: 2025-01-18 09:02:50

IOS第15天(2,事件处理,侧滑菜单,抽屉效果)的相关文章

老二牛车Axure夜话:Axure手机原型视频教程之侧滑菜单(抽屉导航)

老二牛车Axure夜话:Axure手机原型视频教程之侧滑菜单(抽屉导航) 案例描述:侧滑菜单(抽屉导航) 知识点: 动态面板移动 效果图: 本站在线效果预览:http://www.iniuche.com/phonelesson/drawer/start.html#p=home(firefox原型文件) AxShare在线效果预览: 原型下载地址:侧滑菜单抽屉导航.rp 在线视频: 实现步骤: 更新中…..

ViewDragHelper实践之仿Android官方侧滑菜单NavigationDrawer效果

相信经常使用移动应用的用户都很熟悉侧滑菜单栏, 下拉, 下弹, 上弹等应用场景, 几乎主流的移动应用无论IOS 还是Android都能看到. 2.3以前的时候, 很多第三方比如SlidingMenu, MenuDrawer, ActionbarSherlock等等都很大程度的丰富和深化了这种交互理念.能让小小的屏幕, 容纳更多的交互接口. 也是这种趋势, Android官方在v4终于推出了DrawerLayout. 表示对侧滑的重视与肯定. 唠叨到这了. 去看了DrawerLayout的源码和官

Android-自定义滑动菜单(抽屉效果)

在Andoird使用Android自带的那些组件,像SlidingDrawer和DrawerLayout都是抽屉效果的菜单,但是在项目很多要实现的功能都收到Android这些自带组件的限制,导致很难完成项目的需求,自定义的组件,各方面都在自己的控制之下,从而根据需求做出调整.想要实现好的效果,基本上都的基于Android的OnTouch事件自己实现响应的功能. 首先,给大家先看一下整体的效果: 滑动的加速度效果都是有的,具体的体验,只能安装后才能查看. 接下来,看代码: 代码从MainActiv

IOS第15天(2,事件处理hitTest练习)

***hitTest 获取最合适的点 @implementation HMGreenView - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"%s",__func__); } // 获取 最合适的 点的view //- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event //{ // // 把自己的点转换按钮的坐标系上

IOS第15天(3,事件处理,手势处理)

7> 手势识别    使用UIImageView原因:之前既能看见图片,又能监听点击的只有UIButton,学了手势,我们的UIImageView也可以.    * tap(代理:左边不能点,右边能点)    * longPress(allowableMovement:触发之前,最大的移动范围)        > 默认调用两次,开始一次,结束一次.    * swipe:(一个手势只能识别一个方向)    * 旋转:      基于上一次旋转    * 复位:(手势的取值都是相对最原始的位置,

IOS第15天(1,事件处理View的拖拽)

*******view 一些方法 #import "HMView.h" @implementation HMView // 一个完整的触摸过程 // touchesBegan -> touchesMoved -> touchesEnded /* NSArray 集合 有序 NSSet 无序 */ // 触摸开始 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 获取一个UITouch

自定义侧滑菜单

类似于QQ侧滑菜单的效果,这个最重要的就是改变摆放方式,从而达到自己想要的效果,首先先弄明白onLayout方法里的参数 这个自定义里最主要的就是通过touch事件来移动显示的范围,移动viewgroup主要有几个方法,onLayout offsetTopAndBottom(offset)和offsetLeftAndRight(offset); scrollTo和scrollBy方法; 注意:滚动的并不是viewgroup内容本身,而是它的矩形边框 它是瞬间移动, 这里我们用的是scrollTo

Android侧滑菜单完整详细示例(精装版)

MainActivity如下: package cn.patience7; import android.os.AsyncTask; import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view

QQ5.0侧滑菜单

QQ5.0侧滑菜单 功能分类:社交    支持平台:Android    运行环境:Eclipse 开发语言:Java    开发工具:Eclipse     源码大小:1.06MB 源码简介 和 QQ 侧滑菜单类似效果,包括缩放.透明度变化.位置变化. 下载地址:http://www.dwz.cn/wYBof 源码运行截图