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

iOS开发从入门到精通--UIGesture手势基础

程序员文章站 2022-11-10 22:08:07
uigesture手势基础 #import @interface viewcontroller : uiviewcontroller{ //定义一个视图对象 u...

uigesture手势基础
iOS开发从入门到精通--UIGesture手势基础

#import 

@interface viewcontroller : uiviewcontroller{

    //定义一个视图对象
    uiimageview * _imageview;
}


@end
#import "viewcontroller.h"

@interface viewcontroller ()

@end

@implementation viewcontroller

- (void)viewdidload {
    [super viewdidload];
    // do any additional setup after loading the view, typically from a nib.

    //加载图像对象,从本地加载到内存
    uiimage * image =[uiimage imagenamed:@"17_2.png"];

    //创建图像视图
    _imageview = [[uiimageview alloc]init];

    //将图像视图的图像赋值
    _imageview.image = image;
    _imageview.frame =cgrectmake(50, 80, 200, 300);

    [self.view addsubview:_imageview];


    //开启交互事件响应开关
    //yes:可以响应交互事件
    //no:不能接受响应事件,默认值为no;
    _imageview.userinteractionenabled=yes;

    //创建一个点击手势对象
    //uitapgesturerecognizer:点击手势类
    //功能:识别点击手势事件
    //p1:响应事件的拥有者对象,self表示当前视图控制器
    //p2:响应事件的函数
    uitapgesturerecognizer * taponeges = [[uitapgesturerecognizer alloc]initwithtarget:self action:@selector(taponeact:)];

    //表示手势识别事件的事件类型:几次点击时触发
    //默认值为1
    taponeges.numberoftapsrequired=1;
//    taponeges.numberoftapsrequired=2;

    //表示几个手指点击时触发此事件函数
    //默认值为1
    taponeges.numberoftouchesrequired=1;

    //将点击事件添加到视图中,视图即可响应事件
    [_imageview addgesturerecognizer:taponeges];

    uitapgesturerecognizer * taptwoges = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(taptwoges:)];

    taptwoges.numberoftapsrequired=2;
    taptwoges.numberoftouchesrequired=1;
    [_imageview addgesturerecognizer:taptwoges];

    //当单击操作遇到双击操作时,单击操作失效
    [taponeges requiregesturerecognizertofail:taptwoges];
}

//事件响应函数,单击操作
//参数手势点击事件对象
-(void) taponeact:(uitapgesturerecognizer*)tap{
    nslog(@"单击操作!");
    //获取手势监控的视图对象
    uiimageview * imageview = (uiimageview*)tap.view;
    //开始动画过程
    [uiview beginanimations:nil context:nil];
    //设置动画过度时间
    [uiview setanimationduration:2];

    imageview.frame=cgrectmake(0, 0, 320, 568);

    //结束动画过程
    [uiview commitanimations];

}

//双击操作
-(void)taptwoges:(uitapgesturerecognizer*)tap{
    nslog(@"双击操作");
    [uiview beginanimations:nil context:nil];
    [uiview setanimationduration:1];
    _imageview.frame =cgrectmake(50, 80, 200, 300);
    [uiview commitanimations];


}

- (void)didreceivememorywarning {
    [super didreceivememorywarning];
    // dispose of any resources that can be recreated.
}

@end