#import "CustomView.h"
@implementation CustomView
/**
* 手指点击屏幕会自动触发
*
* @param touches touches description
* @param event event description
*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
// NSLog(@"111111111触摸开始");
}
/**
* 手指移动会自动触发
*
* @param touches touches description
* @param event event description
*/
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
// NSLog(@"111111111触摸移动");
}
/**
* 手指离开屏幕出触发
*
* @param touches touches description
* @param event event description
*/
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
// NSLog(@"111111111触摸结束");
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@end
#import "ViewController.h"
#import "CustomView.h"
#import "UILabel+Addition.h"
@interface ViewController ()
@property (nonatomic, weak) CustomView *customView;
#if 0
@property (nonatomic, weak) UILabel *l1;
@property (nonatomic, weak) UILabel *l2;
@property (nonatomic, weak) UILabel *l3;
@property (nonatomic, weak) UILabel *l4;
#endif
@end
@implementation ViewController
#if 0
- (UILabel *)l2
{
if (_l2)
{
UILabel *l = [UILabel label];
[self.view addSubview:l];
_l2 = l;
}
return _l2;
}
- (UILabel *)l1
{
if (!_l1)
{
UILabel *l = [UILabel labelWithFrame:self.view.frame textColor:[UIColor redColor] textAlignment:NSTextAlignmentCenter];
[self.view addSubview:l];
_l1 = l;
}
return _l1;
}
#endif
- (CustomView *)customView
{
if (!_customView)
{
CustomView *v = [[CustomView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
v.backgroundColor = [UIColor redColor];
[self.view addSubview:v];
_customView = v;
}
return _customView;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/*
UIView以及UIView子类和UIViewController都是继承UIResponser
*/
[self customView];
}
/**
* 手指点击屏幕会自动触发
*
* @param touches touches description
* @param event event description
*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"222222222触摸开始");
//1.获取触摸对象
UITouch *touch = [touches anyObject];
//2.获取手机点击屏幕的点坐标
CGPoint center = [touch locationInView:self.view];
NSLog(@"---%@",NSStringFromCGPoint(center));
//3.修改红色视图的中心点
self.customView.center = center;
}
/**
* 手指移动会自动触发
*
* @param touches touches description
* @param event event description
*/
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"22222222222触摸移动");
//1.获取触摸对象
UITouch *touch = [touches anyObject];
//2.获取手机点击屏幕的点坐标
CGPoint center = [touch locationInView:self.view];
NSLog(@"---%@",NSStringFromCGPoint(center));
//3.修改红色视图的中心点
self.customView.center = center;
}
/**
* 手指离开屏幕出触发
*
* @param touches touches description
* @param event event description
*/
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"22222触摸结束");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end