欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

ios触屏事件指南

程序员文章站 2022-06-09 19:45:20
复制代码 代码如下: //在一个函数里面(初始化等)里面添加要识别触摸事件的范围     infoview=[[uiview alloc]...

复制代码 代码如下:

//在一个函数里面(初始化等)里面添加要识别触摸事件的范围
    infoview=[[uiview alloc] initwithframe:cgrectmake(20, 100,220, 280)];//范围,出了这个范围就检测不到触摸了
    infoview.backgroundcolor=[uicolor bluecolor];
    infoview.alpha=0.6;
    [self.view addsubview:infoview];
    /******************监视手势控制*****************///有上下左右,我只用到了左右,上下注掉了。
    uiswipegesturerecognizer *recognizer;
    recognizer = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(handleswipefrom:)];
    [recognizer setdirection:(uiswipegesturerecognizerdirectionright)];
    [infoview addgesturerecognizer:recognizer];
//    recognizer = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(handleswipefrom:)];
//    [recognizer setdirection:(uiswipegesturerecognizerdirectionup)];
//    [self.view addgesturerecognizer:recognizer];
//    recognizer = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(handleswipefrom:)];
//    [recognizer setdirection:(uiswipegesturerecognizerdirectiondown)];
//    [self.view addgesturerecognizer:recognizer];
    recognizer = [[uiswipegesturerecognizer alloc] initwithtarget:self action:@selector(handleswipefrom:)];
    [recognizer setdirection:(uiswipegesturerecognizerdirectionleft)];
    [infoview addgesturerecognizer:recognizer];
//触摸事件 的实现函数
//滑动事件1
-(void)handleswipefrom:(uiswipegesturerecognizer *)recognizer{
    //如果往左滑
    if(recognizer.direction==uiswipegesturerecognizerdirectionleft) {
        nslog(@"****************向左滑****************");
    }
    //如果往右滑
    if(recognizer.direction==uiswipegesturerecognizerdirectionright) {
        nslog(@"****************向右滑****************");      
    }
   if (recognizer.direction==uiswipegesturerecognizerdirectiondown){
       nslog(@"****************向下滑****************");
    }
    if (recognizer.direction==uiswipegesturerecognizerdirectionup){
      nslog(@"****************向上滑****************");
   }
}

方法二:

复制代码 代码如下:

gseventrecord header;    
gshardwarekeyinfo key = {0,0,0,0,1,{'a'},1,{'a'},0,0,0,0};    
memset(&header, 0, sizeof(header));    
header.type = kgseventkeydown;    
header.infosize = sizeof(gshardwarekeyinfo);    
header.timestamp = mach_absolute_time();    
struct    
{    
    gseventrecord header1;    
    gshardwarekeyinfo key1;    
}fuck = {header, key};    
gssendevent(&fuck, gsgetpurpleapplicationport());   
    gseventrecord header; 
    gshardwarekeyinfo key = {0,0,0,0,1,{'a'},1,{'a'},0,0,0,0}; 
    memset(&header, 0, sizeof(header)); 
    header.type = kgseventkeydown; 
    header.infosize = sizeof(gshardwarekeyinfo); 
    header.timestamp = mach_absolute_time(); 
    struct 
    { 
        gseventrecord header1; 
        gshardwarekeyinfo key1; 
    }fuck = {header, key}; 
    gssendevent(&fuck, gsgetpurpleapplicationport());

以上所述就是本文的全部内容了,希望大家能够喜欢。