1、監(jiān)聽手勢(shì)滑動(dòng)的方法
1-1摊聋、監(jiān)聽手勢(shì)開始接觸屏幕方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
1-2隙券、監(jiān)聽手勢(shì)在屏幕上滑動(dòng)方法
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
1-3榜聂、監(jiān)聽手勢(shì)滑動(dòng)結(jié)束
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
2、完整Demo
2-1:通過手勢(shì)的滑動(dòng)來改變背景顏色的 alpha值
//
// ViewController.m
// 66-手勢(shì)滑動(dòng)
//
// Created by freedom on 16/10/17.
// Copyright ? 2016年 Raven. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
/**
* 第一個(gè)點(diǎn)
*/
@property (nonatomic, assign) CGPoint firstPoint;
/**
* 第二個(gè)點(diǎn)
*/
@property (nonatomic, assign) CGPoint secondPoint;
/**
* 最開始的點(diǎn)
*/
@property (nonatomic, assign) CGPoint startPoint;
/**
* 滾動(dòng)條
*/
@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 監(jiān)聽手勢(shì)方法
#pragma mark 1-開始接觸屏幕
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//第一個(gè)點(diǎn)
for (UITouch *touch in event.allTouches) {
self.firstPoint = [touch locationInView:self.view];
}
self.startPoint = self.firstPoint;
}
#pragma mark 2-手勢(shì)在屏幕上滑動(dòng)
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//第二個(gè)點(diǎn)
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-手勢(shì)在屏幕上滑動(dòng)結(jié)束
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.firstPoint = self.secondPoint = CGPointZero;
}
@end
3陌凳、小結(jié)
3-1剥懒、之所以使用UISlider是因?yàn)閁ISlider的值是從0~1來變化的,所以個(gè)人覺得合敦,如果以后有什么需要線性變化都可以使用UISlider
3-2初橘、演示時(shí)發(fā)現(xiàn),如果UISlider顯示出來(具有frame、hidden=NO)時(shí)保檐,會(huì)發(fā)現(xiàn)具有UISlider區(qū)域的上面下面就不會(huì)具有滑動(dòng)時(shí)間耕蝉,所以建議隱藏UISlider
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者