UI基础整理-4

触摸事件:

开始触摸:touchesbegan

移动触摸:touchesmoved

结束触摸:touchesended

取消触摸:touchescanceled


//开始触摸

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    }

//移动触摸

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{    }

//结束触摸
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{    }

//取消触摸

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{    }


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//获取触摸设备的对象
        UITouch *touch = [touches anyObject];
//获取前一个点
        CGPoint p1 = [touch previousLocationInView:self];                      

//获取当前的点      

        CGPoint p2 = [touch locationInView:self];
//根据两个点的横纵向距离来计算出移动结果,并将值赋给self.center更新位置
        self.center = CGPointMake(p2.x - p1.x + self.center.x, p2.y - p1.y + self.center.y);


//UILabel、UIImageView的用户交互默认是关闭的


responer:

处理过程 : 被触摸视图 –> 传给父视图 –> View –> 视图控制器 –>window –>application …遗失

查询过程 : 硬件设备–> Application –> window –> 视图控制器 –> View –> 视图的各个子视图 最终找到被触摸的视图


防止在滑动屏幕的时候划出去
// 屏幕尺寸
#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
// 视图边长的一半
#define kHalfSide self.bounds.size.width/ 2
// 左极限
#define LeftMargin kHalfSide
// 右极限
#define RightMargin kWidth - kHalfSide
// 上极限
#define TopMargin kHalfSide
// 下极限
#define BottomMargin kHeight - kHalfSide

   
   
if (self.center.x<LeftMargin) {
       
CGPoint p = CGPointMake(LeftMargin+1,self.center.y);
       
self.center= p;
    }
elseif(self.center.x>RightMargin){
       
CGPoint p = CGPointMake(RightMargin-1,self.center.y);
       
self.center= p;
    }
elseif(self.center.y<TopMargin){
       
CGPoint p = CGPointMake(self.center.x,TopMargin+1);
       
self.center= p;
    }
elseif(self.center.y>BottomMargin){
       
CGPoint p = CGPointMake(self.center.x,BottomMargin-1);
       
self.center= p;
    }
else{
             //获取触碰设备的对象
            UITouch*touch = [touches anyObject];
             //获取前一个点
            CGPointp1 = [touch previousLocationInView:self];
             //获取当前的点
            CGPointp2 = [touch locationInView:self];
       
            //根据两个点的横纵向距离来计算出移动结果,并将值赋给self.center更新位置
            self.center=CGPointMake(p2.x-p1.x+self.center.x, p2.y-p1.y+self.center.y);
    }





你可能感兴趣的:(UI基础整理)