触摸事件

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值