04-手势滑动(把屏幕分成四个区域)

1、本篇继续研究手势滑动,不同的是把屏幕分成四个手势滑动区域

2、Demo通过滑动四个不同的区域来控制背景色的RGB值和透明度

//
//  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;
/**
 *  控制alider
 */
@property (nonatomic, strong) UISlider *alider;

/**
 *  控制背景色RGB中的R
 */
@property (nonatomic, strong) UISlider *R_slider;

/**
 *  控制背景色RGB中的G
 */
@property (nonatomic, strong) UISlider *G_slider;

/**
 *  控制背景色RGB中的B
 */
@property (nonatomic, strong) UISlider *B_slider;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //1、颜色的透明度
    self.alider = [[UISlider alloc] initWithFrame:CGRectMake(0, -100, 0, 0)];
    //设置最小值
    self.alider.minimumValue = 0.0;
    //设置最大值
    self.alider.maximumValue = 1.0;
    self.alider.hidden = YES;
//    self.alider.value = [UIScreen mainScreen].brightness;
    [self.view addSubview:self.alider];
    
    //2、控制背景色RGB中的R
    self.R_slider = [[UISlider alloc] initWithFrame:CGRectMake(0, -100, 0, 0)];
    //设置最小值
    self.R_slider.minimumValue = 0.0;
    //设置最大值
    self.R_slider.maximumValue = 1.0;
    self.R_slider.hidden = YES;
//    self.R_slider.value = [UIScreen mainScreen].brightness;
    [self.view addSubview:self.R_slider];
    
    //3、控制背景色RGB中的G
    self.G_slider = [[UISlider alloc] initWithFrame:CGRectMake(0, -100, 0, 0)];
    //设置最小值
    self.G_slider.minimumValue = 0.0;
    //设置最大值
    self.G_slider.maximumValue = 1.0;
    self.G_slider.hidden = YES;
    //    self.R_slider.value = [UIScreen mainScreen].brightness;
    [self.view addSubview:self.G_slider];
    
    
    //4、控制背景色RGB中的B
    self.B_slider = [[UISlider alloc] initWithFrame:CGRectMake(0, -100, 0, 0)];
    //设置最小值
    self.B_slider.minimumValue = 0.0;
    //设置最大值
    self.B_slider.maximumValue = 1.0;
    self.B_slider.hidden = YES;
    //    self.R_slider.value = [UIScreen mainScreen].brightness;
    [self.view addSubview:self.B_slider];
    
}

#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];
    }
    
    //判断是左右滑动还是上下滑动
    CGFloat verValue =fabs(self.startPoint.y - self.secondPoint.y);
    CGFloat horValue = fabs(self.startPoint.x - self.secondPoint.x);
    
    //左右分屏
    if (self.startPoint.x <= self.view.frame.size.width * 0.5) {
        
#warning 左屏上下滚动
        if (verValue < horValue) return;
        
        //区分上下屏
        if (self.startPoint.y <= self.view.frame.size.height * 0.5) {
#pragma mark 1-左上拼来控制 背景色的alpha值
            self.alider.value += (self.firstPoint.y - self.secondPoint.y) / 500;
            NSLog(@"左上屏alpha:%f", self.alider.value);
            self.view.backgroundColor = [UIColor colorWithRed:self.R_slider.value green:self.G_slider.value blue:self.B_slider.value alpha:self.alider.value];
        } else {
#pragma mark 2-左下屏来控制 背景色的R值
            self.R_slider.value += (self.firstPoint.y - self.secondPoint.y) / 500;
            NSLog(@"左下屏R:%f", self.R_slider.value);
            self.view.backgroundColor = [UIColor colorWithRed:self.R_slider.value green:self.G_slider.value blue:self.B_slider.value alpha:self.alider.value];
        }
        
    } else {
#warning 右屏左右滚动
        if (verValue > horValue) return;
        
        
        //区分上下屏
        if (self.startPoint.y <= self.view.frame.size.height * 0.5) {
#pragma mark 3-左下屏来控制 背景色的G值
            self.G_slider.value += (self.firstPoint.x - self.secondPoint.x) / 500;
            NSLog(@"右上屏G:%f", self.G_slider.value);
            
            self.view.backgroundColor = [UIColor colorWithRed:self.R_slider.value green:self.G_slider.value blue:self.B_slider.value alpha:self.alider.value];
            
        } else {
#pragma mark 4-左下屏来控制 背景色的G值
            self.B_slider.value += (self.firstPoint.x - self.secondPoint.x) / 500;
            NSLog(@"右下屏B:%f", self.B_slider.value);
            
            self.view.backgroundColor = [UIColor colorWithRed:self.R_slider.value green:self.G_slider.value blue:self.B_slider.value alpha:self.alider.value];
        }
    }
    
    self.firstPoint = self.secondPoint;
}

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


@end

你可能感兴趣的:(04-手势滑动(把屏幕分成四个区域))