博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS中的各种手势
阅读量:4079 次
发布时间:2019-05-25

本文共 4397 字,大约阅读时间需要 14 分钟。

/**
 基类UIGestureRecognizer
 UITapGestureRecognizer        Tap 点击
 UIPanGestureRecognizer        Pan (慢速滑动,拖移)
 UILongPressGestureRecognizer  LongPress (长按)
 UIPinchGestureRecognizer      Pinch (捏合,两手指往内或外拨动)
 UIRotationGestureRecognizer   Rotation (旋转)
 UISwipeGestureRecognizer        Swipe (快速滑动,轻扫)
 */
#import "ViewController.h"
#define kRandomValue (arc4random() %256 /256.0)
@interface ViewController () <UIGestureRecognizerDelegate>
@property (nonatomic,strong) UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //强烈不建议往一个控件上加很多手势
    [self addImageView];
    [self addTapGesture];//点击手势
    //[self addPanGesture];//拖移手势
    [self addLongPressGesture];//长按手势
    [self addPinchGesture];//捏合
    [self addResetButton];//重置
    [self addRotationGesture];//旋转
    [self addSwipeGesture];//轻扫
}
-(void)addImageView{
    UIImage *image=[UIImage imageNamed:@"cloth"];
    self.imageView=[[UIImageView alloc] initWithImage:image];//这样写默认imageView的大小和image的大小一样
    self.imageView.userInteractionEnabled=YES;//打开用户交互
    self.imageView.center=self.view.center;
    [self.view addSubview:self.imageView];
}
-(void)addTapGesture{
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] init];
    //tap.numberOfTapsRequired=2;//默认为1,点击次数
    //tap.numberOfTouchesRequired=2;//默认为1,多少根手指触摸
    [tap addTarget:self action:@selector(tap:)];
    [self.imageView addGestureRecognizer:tap];
}
-(void)tap:(UITapGestureRecognizer *)tap{
    UIView *view=tap.view;
    view.superview.backgroundColor=[UIColor colorWithRed:kRandomValue green:kRandomValue blue:kRandomValue alpha:1.0];
}
-(void)addPanGesture{
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] init];
    pan.delegate=self;
    [pan addTarget:self action:@selector(pan:)];
    [self.imageView addGestureRecognizer:pan];
}
-(void)pan:(UIPanGestureRecognizer *)pan{
    CGPoint offset=[pan
                    translationInView:self.view];
    CGPoint center=self.imageView.center;
    center.x+=offset.x;
    center.y+=offset.y;
    self.imageView.center=center;
    
    //不让其累加
    [pan setTranslation:CGPointZero inView:self.view];
}
-(void)addLongPressGesture{
    UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc ] init];
    [longPress addTarget:self action:@selector(longPress:)];
    [self.imageView addGestureRecognizer:longPress];
}
-(void)longPress:(UILongPressGestureRecognizer *)longPress{
    switch (longPress.state) {
        case UIGestureRecognizerStateBegan:
            NSLog(@"你长按了");
            break;
        case UIGestureRecognizerStateEnded:
            NSLog(@"长按结束");
            break;
        default:
            break;
    }
}
-(void)addPinchGesture{
    UIPinchGestureRecognizer *pinch=[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    [self.imageView addGestureRecognizer:pinch];
}
-(void)pinch:(UIPinchGestureRecognizer *)pinch{
    NSLog(@"%lf",pinch.scale);
    //捏合  scale缩放比例
    //transform 变换,仿射变换/矩阵变换,
    self.imageView.transform=CGAffineTransformScale(self.imageView.transform, pinch.scale, pinch.scale);
    //重置捏合手势比例,每次都应该相对于当前的
    pinch.scale=1;//下次相对比例应该从1开始
}
-(void)addResetButton{
    UIButton *button=[UIButton buttonWithType:UIButtonTypeSystem];
    button.frame=CGRectMake(0, CGRectGetMaxY(self.imageView.frame)+60, 200, 40);
    [button setTitle:@"Reset" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(reset:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
-(void)reset:(id)sender{
    //恢复imageView的transform
    self.imageView.transform=CGAffineTransformIdentity;
}
-(void)addRotationGesture{
    UIRotationGestureRecognizer *rotation=[[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    [self.imageView addGestureRecognizer:rotation];
}
-(void)rotation:(UIRotationGestureRecognizer *)rotation{
    NSLog(@"旋转弧度:%lf",rotation.rotation);
    self.imageView.transform=CGAffineTransformRotate(self.imageView.transform, rotation.rotation);
    rotation.rotation=0;//重置旋转弧度,相当于当前的从0开始
}
-(void)addSwipeGesture{
    UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
    //swipe默认的direction为right
    swipe.direction=UISwipeGestureRecognizerDirectionUp;
    [self.imageView addGestureRecognizer:swipe];
}
-(void)swipe:(UISwipeGestureRecognizer *)swipe{
    CGPoint center=self.imageView.center;
    center.y -=50;
    self.imageView.center=center;
}
#pragma mark -UIGestureRecognizerDelegate
//是否支持多个手势
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}
@end

转载地址:http://pjsni.baihongyu.com/

你可能感兴趣的文章
树莓派默认的用户名是pi 默认的密码是raspberry,用这个用户名和密码去远程登陆
查看>>
现在终于知道realsense官方的树莓派安装教程了
查看>>
在树莓派上装librealsense我找到两个官方教程了,一个是realsense给的,一个是APM给的T265的教程
查看>>
树莓派的系统是烧录到SD卡直接启动可以用了,那我之前买的ubuntu的U盘系统应该原理类似,那烧录方法是不是应该也是类似?
查看>>
GNU nano是Shell中常用的一款文本编辑器
查看>>
Raspbian是树莓派官方推出的操作系统
查看>>
树莓派操作系统的烧录
查看>>
没有屏幕如何连接树莓派
查看>>
没有屏幕和键盘如何玩转树莓派
查看>>
腾达的路由器直接浏览器访问 192.168.0.1就可以看到各个设备的IP地址了,树莓派的应该也可以。
查看>>
树莓派设置自动连wifi
查看>>
树莓派一根网线直连笔记本电脑(这样或许真的不需要再单独买个屏幕了)
查看>>
软件装好后如何开启并控制无人机
查看>>
我发现APM的官方手册还给了很多其他板子的教程,也很详细,这个真的比PX4的官方手册好得多!
查看>>
linux里用 lsusb 命令查看USB串口信息
查看>>
APM官方教程的视频的作者用的固件版本是3.6.9稳定版,苍穹四轴/阿木他们那个APM树莓派T265用的3.6.11版本的固件
查看>>
APM官方固件下载
查看>>
树莓派+英特尔神经网络计算棒
查看>>
我已经多次看到猛禽360机架了
查看>>
T265在无人机上的固定
查看>>