03-手势滑动

1、监听手势滑动的方法

1-1、监听手势开始接触屏幕方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

1-2、监听手势在屏幕上滑动方法

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

1-3、监听手势滑动结束

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

2、完整Demo

2-1:通过手势的滑动来改变背景颜色的 alpha值

//
//  ViewController.m
//  66-手势滑动
//
//  Created by freedom on 16/10/17.
//  Copyright © 2016年 Raven. All rights reserved.
//

#import "ViewController.h"
@interface ViewController ()
/**
 *  第一个点
 */
@property (nonatomic, assign) CGPoint firstPoint;

/**
 *  第二个点
 */
@property (nonatomic, assign) CGPoint secondPoint;
/**
 *  最开始的点
 */
@property (nonatomic, assign) CGPoint startPoint;
/**
 *  滚动条
 */
@property (nonatomic, strong) UISlider *alider;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGFloat w = self.view.frame.size.width;
    CGFloat h = self.view.frame.size.height;
    
    
    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(w-100, 100, 100, 100)];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    
    
    self.alider = [[UISlider alloc] initWithFrame:CGRectMake(0, 100, w-300, h)];
    self.alider.minimumValue = 0.0;
    self.alider.maximumValue = 1.0;
    self.alider.hidden = NO;
    self.alider.value = [UIScreen mainScreen].brightness;
    [self.view addSubview:self.alider];
}

#pragma mark -
#pragma mark 监听手势方法
#pragma mark 1-开始接触屏幕
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //第一个点
    for (UITouch *touch in event.allTouches) {
        self.firstPoint = [touch locationInView:self.view];
    }
    
    self.startPoint = self.firstPoint;
    
}

#pragma mark 2-手势在屏幕上滑动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    //第二个点
    for (UITouch *touch in event.allTouches) {
        self.secondPoint = [touch locationInView:self.view];
    }
    self.alider.value += (self.firstPoint.y - self.secondPoint.y) / 500;
    NSLog(@"%f", self.alider.value);
    
    self.view.backgroundColor = [UIColor colorWithRed:self.alider.value green:0.0 blue:0.0 alpha:1.0];
    self.firstPoint = self.secondPoint;
}

#pragma mark 3-手势在屏幕上滑动结束
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    self.firstPoint = self.secondPoint = CGPointZero;
}


@end

3、小结

3-1、之所以使用UISlider是因为UISlider的值是从0~1来变化的,所以个人觉得,如果以后有什么需要线性变化都可以使用UISlider

3-2、演示时发现,如果UISlider显示出来(具有frame、hidden=NO)时,会发现具有UISlider区域的上面下面就不会具有滑动时间,所以建议隐藏UISlider

你可能感兴趣的:(03-手势滑动)