zoukankan      html  css  js  c++  java
  • iOS触摸事件

    {//开始触摸点
        CGPoint _startPoint;

    }

    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
            //给testVIew加上颜色
            self.backgroundColor=[UIColor redColor];
            //阻断
             //self.userInteractionEnabled=NO;
            
            
        }
        return self;
        
    }

    //有什么吩咐开始出发触摸事件的时候,执行touchBegan里面的预定的执行事件代码(开始触摸的时候,到这里来看看.)
    //一次触摸事件发生时,该方法只执行一次
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"我要开始摸了.");
        self.backgroundColor=[UIColor greenColor];//点击改变颜色
        self.backgroundColor=[UIColor colorWithRed:kRandomColor green:kRandomColor blue:kRandomColor alpha:1];
        //取出手指触控屏幕的坐标
    //    CGPoint sp=[[touches anyObject] locationInView:self];
    //    NSLog(@"%@",NSStringFromCGPoint(sp));
        _startPoint=[[touches anyObject] locationInView:self];
        NSLog(@"%@",NSStringFromCGPoint(_startPoint));
        
    }


    //一次触摸事件尚未结束,会一直调用该方法(没摸完,就一直摸)
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"触摸ing");
        self.backgroundColor=[UIColor colorWithRed:kRandomColor green:kRandomColor blue:kRandomColor alpha:1];//点击移动一直改变颜色
        //取移动中的点
        CGPoint nowPoint=[[touches anyObject] locationInView:self];
        NSLog(@"%@",NSStringFromCGPoint(nowPoint));
        CGFloat x=nowPoint.x-_startPoint.x;
        CGFloat y=nowPoint.y-_startPoint.y;
        CGPoint centerPoint=CGPointMake(self.center.x+x,self.center.y+y);
        self.center=centerPoint;
        
    }


    //一次触摸事件结束,执行该方法(摸完了)
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"结束了.");
        
    }


    //触摸事件被别的事件打断(有人打扰)
    -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    {

    }

  • 相关阅读:
    Day8 类的继承
    Day7 访问权限
    javascript性能优化
    JavaScript 基础:Babel 转译 class 过程窥探
    HTML5之新增的元素和废除的元素 (声明:内容节选自《HTML 5从入门到精通》)
    谈起音视频,前端能做些什么
    Safari不能保存session的处理方法
    一篇关于BEM命名规范
    H5新人福音~零配置搭建现代化的前端工程
    2018年,最经典的26个JavaScript面试题和答案!
  • 原文地址:https://www.cnblogs.com/-ios/p/4672656.html
Copyright © 2011-2022 走看看