关于IOS手势

#import <UIKit/UIKit.h>


@interface AppDelegate : UIResponder <UIApplicationDelegate>


@property (strong, nonatomic) UIWindow *window;



@end




//

//  AppDelegate.m

//  UI05_UIGestureRecognizer

//

//  Created by dlios on 15-5-20.

//  Copyright (c) 2015 lanou3g.com 蓝鸥科技. All rights reserved.

//


#import "AppDelegate.h"

#import "MainViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    [_window release];

    

    MainViewController *mainVC = [[MainViewController alloc] init];

    self.window.rootViewController = mainVC;

    [mainVC release];

    

    

    return YES;

}


- (void)dealloc

{

    [_window release];

    [super dealloc];

}


@end



#import <UIKit/UIKit.h>


@interface MainViewController : UIViewController


@end




//

//  MainViewController.m

//  UI05_UIGestureRecognizer

//

//  Created by dlios on 15-5-20.

//  Copyright (c) 2015 lanou3g.com 蓝鸥科技. All rights reserved.

//


#import "MainViewController.h"


@interface MainViewController ()


@end


@implementation MainViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    // UIImageView的使用

    

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 357, 667)];

    imageView.backgroundColor = [UIColor yellowColor];

    [self.view addSubview:imageView];

    [imageView release];

    

    // 图片类,一个对象就代表一个图片

    UIImage *image = [UIImage imageNamed:@"1.png"];

    

    // imageView赋值,将图片显示出来

    imageView.image = image;

    

    

    //打开imageView的视图交互,imageView可以响应用户事件

    imageView.userInteractionEnabled = YES;

   

    

   // 手势

    

    // 系统提供了7种手势:

   //  1.点击 2.轻扫 3.拖动 4.缩放 5.旋转 6.长按 7.屏幕边缘拖拽

    

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

    

    // 设置手势的属性

    tap.numberOfTapsRequired = 2;

    

    // 需要几个手指点击view

    tap.numberOfTouchesRequired = 2;

    

    // imageView添加一个手势,imageView可以响应点击

    [imageView addGestureRecognizer:tap];

    [tap release];

    

    

    

    

     //长按 (LongPress)

    

    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];

    

    // 判定长按手势需要的时间

    longPress.minimumPressDuration = 2;

    

    // 允许用户在长按的判定时间内移动手指的距离 default = 10

    longPress.allowableMovement = 100;

    

    [imageView addGestureRecognizer:longPress];

    [longPress release];

    

    

    // 轻扫(swipe)

    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];

    

    [imageView addGestureRecognizer:swipe];

    [swipe release];

    

    //判定轻扫方向 若想四个方向 4swipe

    swipe.direction = UISwipeGestureRecognizerDirectionLeft;

    

    // 旋转(rotation)

    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];

    [imageView addGestureRecognizer:rotation];

    [rotation release];


    // 缩放(pinch)

    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];

    [imageView addGestureRecognizer:pinch];

    [pinch release];

    

   // 拖拽(Pan)

    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];

    [imageView addGestureRecognizer:pan];

    [pan release];

    

  

    

}


- (void)panAction:(UIPanGestureRecognizer *)pan

{

    NSLog(@"拖拽");

    UIImageView *imageView = (UIImageView *)pan.view;

    

    //获取拖拽的相对点

    CGPoint point = [pan translationInView:imageView];

    

    imageView.transform = CGAffineTransformTranslate(imageView.transform, point.x, point.y);

    [pan setTranslation:CGPointZero inView:imageView];

}


- (void)pinchAction:(UIPinchGestureRecognizer *)pinch

{

    NSLog(@"缩放");

    UIImageView *imageView = (UIImageView *)pinch.view;

    imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);

    pinch.scale = 1;

}



- (void)rotationAction:(UIRotationGestureRecognizer *)rotation

{

    NSLog(@"旋转");

    

    // 获得这个手势所在的view

    UIImageView *imageView = (UIImageView *)rotation.view;

    imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);

    rotation.rotation = 0;

    

}



- (void)longPressAction:(UILongPressGestureRecognizer *)longpress

{

    // 对手势的状态做判断

    if (longpress.state == UIGestureRecognizerStateBegan) {

        NSLog(@"手势开始");

        

        NSLog(@"长按");

   

    }

}


- (void)swipeAction:(UISwipeGestureRecognizer *)swipe

{

    NSLog(@"轻扫");

}





- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值