zoukankan      html  css  js  c++  java
  • 单视图控制器--手势和图像视图

    ---恢复内容开始---

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        //实例化图片视图对象
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 100, 320, 320)];
        //设置图片背景颜色
        imageView.backgroundColor=[UIColor redColor];
        //将图片视图对象添加到根视图对象上
        [self.view addSubview:imageView];
        //设置图片视图对象的一组动态图片
        imageView.animationImages=[self animationImages];
        imageView.animationDuration=2;//遍历一遍需要的时间
        imageView.animationRepeatCount=-1;//重复次数,-1表示无数次
        [imageView startAnimating];
        
        //实例化开始按钮对象,大小(100,30)位置从根视图坐标系统的原点偏移(110,440),按钮初始文字"开始"
        //按钮触发事件,didClickButton;self,按钮变量名,button
        UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(110, 440, 100, 30)];
        [button setTitle:@"开始" forState:UIControlStateNormal];
        button.backgroundColor=[UIColor orangeColor];
        [button addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];

    }
    -(void)didClickButton:(UIButton *)button
    {
        if ([button.titleLabel.text isEqualToString:@"开始"]) {
            [button setTitle:@"结束" forState:UIControlStateNormal];
            return;
        }
        [button setTitle:@"开始" forState:UIControlStateNormal];
    }
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }

    //加载动态图片
    -(NSArray *)animationImages
    {
        //用来存储图片的名字,路径
        NSMutableArray *array=[NSMutableArray new];
        //程序安装包的根路径
        NSString *rootPath=[[NSBundle mainBundle]resourcePath];
        //图片文件的路径
        NSString *imageDirectiory=[rootPath stringByAppendingString:@"/ImagePackage"];
        //遍历生成40张图片路径
        for (NSInteger index=1; index<=40; index++) {
            NSString *imagePath=[imageDirectiory stringByAppendingFormat:@"/2250_3650903_179fb89ae279fae-%d(被拖移).tiff",index];
            //用文件内容初始化成一张图片
            UIImage *image=[[UIImage alloc]initWithContentsOfFile:imagePath];
            //每一张图片路径添加到数组内
            [array addObject:image];
        }
        return array;
    }
    @end

    UIEvent,事件分三类:触摸事件/晃动事件/远程控制事件

    UIResponder是基类,所有UIView派生出来的UIKit类都源自他,他继承与NSObject

    第一响应者是应用程序中当前负责接受触摸事件的响应者对象.

    响应者链:硬件--响应者--application--window--viewController--View.

    1,在一定时间内关闭事件传递:应用程序调用UIApplication的beginIgnoringInteractionEvents和endIgnoringInteractionEvents

     2,UIGestureRecognizer  tap longTap swipe rotation pan平移 pinch捏合

     //实例化图像视图
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 100, 300, 300)];
        UIImage *image=[UIImage imageNamed:@"test"];
        imageView.image=image;
        imageView.userInteractionEnabled=YES;
       //    UITapGestureRecognizer:UIGestureRecognizer               点击
       //    UISwipeGestureRecognizer:UIGestureRecognizer            轻扫
       //    UILongPressGestureRecognizer:UIGestureRecognizer      长按
       //    UIPanGestureRecognizer:UIGestureRecognizer                拖拽
       //    UIPinchGestureRecognizer:UIGestureRecognizer              捏合
       //    UIRotationGestureRecognizer:UIGestureRecognizer          翻转


        UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]init];
        //点击需要的手指头个数
        [tapGesture setNumberOfTouchesRequired:1];
        //点击需要的次数
        [tapGesture setNumberOfTapsRequired:1];
        //添加手势成功识别后的动作及发送对象
        [tapGesture addTarget:self action:@selector(_tapMethods:)];
        [self.view addSubview:imageView];
       // [imageView addGestureRecognizer:tapGesture];
        
        UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]init];
        [longPress setNumberOfTapsRequired:1];
        [longPress setNumberOfTouchesRequired:1];
         //最小识别时长
        [longPress setMinimumPressDuration:0.1];
        //设置允许的误差
        [longPress setAllowableMovement:12];
        //添加目标动作
        [longPress addTarget:self action:@selector(longMethod:)];
        [imageView addGestureRecognizer:longPress];
    }
    -(void)longMethod:(UILongPressGestureRecognizer *)sender
    {
        NSLog(@"%s",__FUNCTION__);
    }
    -(void)_tapMethods:(UITapGestureRecognizer *)tap
    {
        UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"温馨提示" delegate:Nil cancelButtonTitle:@"cancel" destructiveButtonTitle:@"点击" otherButtonTitles:@"Ok", nil];
        [actionSheet showInView:self.view];   
    }
    最后重点

    如下代码有什么问题? 

    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelOnClick:)];
        tap.numberOfTapsRequired=1;
        tap.numberOfTouchesRequired=1;   
      
        self.label.userInteractionEnabled=YES;
        [self.label addGestureRecognizer:tap];
        [self.button addGestureRecognizer:tap];

    -(void)labelOnClick:sender
    {
        NSLog(@"label on click");
    }

    发现label的手势不起作用,原因有两点:

    1,label默认UserInteractionEnabled为no,需要设置

    [self.label setUserInteractionEnabled:YES];

    2,手势对应一个事件,并且对应一个控件,如果label和button用同一个手势,只有后来使用的才会有效果.

  • 相关阅读:
    Big Data 應用:第二季(4~6月)台湾地区Game APP 变动分布趋势图
    大数据应用:五大地区喜新厌旧游戏APP类别之比较与分析
    Big Data應用:以"玩家意見"之數據分析來探討何謂"健康型線上遊戲"(上)
    Example:PanGu分詞系統-批次匯入新詞
    C#数据类型02--结构
    C#数据类型01--数组
    C#基础知识点
    陌生Layout属性
    LinearLayout(线性布局)
    Android--入门常识
  • 原文地址:https://www.cnblogs.com/huntaiji/p/3429459.html
Copyright © 2011-2022 走看看