前言:最近工作不是很紧张,而学习的态度和状态确实在太好,想学习各种iOS的相关技术,尤其是那种不会的又很能入门的,会让我有那种持续的快感。话不多说,现在开始总结今天早上30分钟学来的新技术。 UITouch。
正文:
UITouch是一个点击响应的类,今天我只说,我使用它做的小Demo。
就是一个当触摸灰色矩形块并按住拖动的时候,矩形块会跟着动,松开手后便会停留的小Demo
代码如下:
//
// ViewController.m
// 12.18关于UITouch的练习
//
// Created by wangyao on 15/12/18.
// Copyright © 2015年 wangyao. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,assign)CGPoint currentPoint;
@property(nonatomic,assign)CGPoint beginPoint;
@property (weak, nonatomic) IBOutlet UIView *View1;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
self.beginPoint = [touch locationInView:self.View1];
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
CGPoint currentLoaction = [touch locationInView:self.View1];
if ((0<self.beginPoint.x)&&( self.beginPoint.x< self.View1.frame.size.width)&&(0<self.beginPoint.y)&&(self.beginPoint.y<self.View1.frame.size.height)) {
CGRect frame = self.View1.frame;
frame.origin.x+= currentLoaction.x-self.beginPoint.x;
frame.origin.y+=currentLoaction.y - self.beginPoint.y;
self.View1.frame = frame;
}
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
自圆其说:
一.
@interface ViewController ()
1.@property(nonatomic,assign)CGPoint currentPoint;
2.@property(nonatomic,assign)CGPoint beginPoint;
3.@property (weak, nonatomic) IBOutlet UIView *View1;
1,2是设置两个属性,用来待会存储开始触碰屏幕的点的位置,和松开手时候的位置。
3是把View里面的灰色区域放入代码中,使其能够使用。
二.
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
1. UITouch * touch = [touches anyObject];
2.self.beginPoint = [touch locationInView:self.View1];
3. [super touchesBegan:touches withEvent:event];
}
这个方法是点击开始就会掉用的方法
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
1,创建了一个UITouh的对象,用于记录触控到屏幕时的诸多属性
2,把这个触控的点的在self.View1的位置的值赋予self.beginPoint;
3,就是继承父类的其它原来的方法,就是只改了这个点,其它的不变的意思
三.
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
1.UITouch * touch = [touches anyObject];
2.CGPoint currentLoaction = [touch locationInView:self.View1];
3.if ((0<self.beginPoint.x)&&( self.beginPoint.x<
self.View1.frame.size.width)&&(0<self.beginPoint.y)&&(self.beginPoint.y<self.View1.frame.size.height))
{
CGRect frame = self.View1.frame;
frame.origin.x+= currentLoaction.x-self.beginPoint.x;
frame.origin.y+=currentLoaction.y - self.beginPoint.y;
self.View1.frame = frame;
}
这段代码同样
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
作为当手指没有离开屏幕,做移动的时候会调用和监测的一个方法
1,2 创建UITouch对象记录触控点
3,
if ((0<self.beginPoint.x)&&( self.beginPoint.x< self.View1.frame.size.width)&&(0<self.beginPoint.y)&&(self.beginPoint.y<self.View1.frame.size.height)) {
1. CGRect frame = self.View1.frame;
2. frame.origin.x+= currentLoaction.x-self.beginPoint.x;
3.frame.origin.y+=currentLoaction.y - self.beginPoint.y;
4. self.View1.frame = frame;
}
if ((0<self.beginPoint.x)&&( self.beginPoint.x< self.View1.frame.size.width)&&(0<self.beginPoint.y)&&(self.beginPoint.y<self.View1.frame.size.height))
这个判断的意思是判断开始点是不是在这个矩形块当中,可能方法优点笨,但是我暂时还没想到更好的办法,如果有我再用。
如果满足了这个点在里面,那么继续下一步
1. CGRect frame = self.View1.frame;
取出View1的frame的值。
2. frame.origin.x+= currentLoaction.x-self.beginPoint.x;
3.frame.origin.y+=currentLoaction.y - self.beginPoint.y;
2,3计算出滑动的当前手指点到之前的点的偏移量,并且赋给赋值给frame
4. self.View1.frame = frame;
最后再把求完的这个frame 给self.View1.frame
实现了拖动View1这个块的功能!