OC手势

#import <UIKit/UIKit.h>

 

@interface AppDelegate : UIResponder <UIApplicationDelegate>

 

@property (strong, nonatomic) UIWindow *window;

 

 

@end

 
 

//

//  AppDelegate.m

//  UI04_手势

//

//  Created by dllo on 16/1/8.

//  Copyright © 2016年 ZHAO. All rights reserved.

//

 

#import "AppDelegate.h"

#import "RootViewController.h"

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

- (void)dealloc

{

    [_window release];

    [super dealloc];

}

 

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

    // Override point for customization after application launch.

    

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

    [self.window makeKeyAndVisible];

    self.window.backgroundColor = [UIColor whiteColor];

    RootViewController *vc = [[RootViewController alloc] init];

    self.window.rootViewController = vc;

    

    

    [vc release];

    [self.window release];

    

    

    return YES;

}

 

- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

 

- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

 

- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

 

- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

 

- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

 

@end

 
 

//

//  CustomView.h

//  UI04_手势

//

//  Created by dllo on 16/1/8.

//  Copyright © 2016年 ZHAO. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface CustomView : UIView

 

@end

 
 

//

//  CustomView.m

//  UI04_手势

//

//  Created by dllo on 16/1/8.

//  Copyright © 2016年 ZHAO. All rights reserved.

//

 

#import "CustomView.h"

 

@implementation CustomView

 

/*

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

    // Drawing code

}

*/

 

@end

 

 

 

 

//

//  RootViewController.h

//  UI04_手势

//

//  Created by dllo on 16/1/8.

//  Copyright © 2016年 ZHAO. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface RootViewController : UIViewController

 

@end

 

//

//  RootViewController.m

//  UI04_手势

//

//  Created by dllo on 16/1/8.

//  Copyright © 2016年 ZHAO. All rights reserved.

//

 

#import "RootViewController.h"

 

@interface RootViewController ()

 

@end

 

@implementation RootViewController

 

 

- (void)dealloc

{ [self.view release];

    [super dealloc];

}

- (void)viewDidLoad {

    [super viewDidLoad];

    

    

    

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];

    imageView.center = self.view.center;

    imageView.image = [UIImage imageNamed:@"3.jpg"];

    

    //开启用户交互

    imageView.userInteractionEnabled = YES;

    

    

    

    [self.view addSubview:imageView];

    

    // 1. 点击、轻拍(tap)

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

    // 执行方法需要点击的次数{

    tap.numberOfTapsRequired = 2;

    // 需要几根手指

    tap.numberOfTouchesRequired = 1;

    // 将手势付给视图

    [imageView addGestureRecognizer:tap];

    

    [tap release];

 

    // 2. 长按(longPress)

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

    // 判定长按手势需要时间, 默认为0.5秒

    longPress.minimumPressDuration = 2;

    // 在判定过程中允许 用户手指移动的距离

    longPress.allowableMovement = 200;

    // 添加长按手势

    [imageView addGestureRecognizer:longPress];

    

     [longPress release];

    

    // 3.捏合(pinch)

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

    

    [imageView addGestureRecognizer:pinch];

    

    [pinch release];

    

    

 

    // 4.拖拽/平移(pan)

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

    [imageView addGestureRecognizer:pan];

    

    [pan release];

    

    

    // 5.旋转(rotation)

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

    [imageView addGestureRecognizer:rotation];

    

    [rotation release];

    

   

    [imageView release];

    

    // Do any additional setup after loading the view.

}

#pragma mark - 轻拍事件

- (void)tapAction:(UITapGestureRecognizer *)tap {

    NSLog(@"轻拍、点击手势");

    NSLog(@"%@", tap.view);

    

    

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

    [imageView setImage:[UIImage imageNamed:@"1.jpg"]];

}

#pragma mark - 长按事件

 

     - (void)longPressAction:(UILongPressGestureRecognizer *)longPress {

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

         [imageView setImage:[UIImage imageNamed:@"3.jpg"]];

     }

#pragma mark - 捏合手势

- (void)pinchAction:(UIPinchGestureRecognizer *)pinch {

    

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

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

    pinch.scale = 1;

}

 

 

 

#pragma  mark - 平移

- (void)panAction:(UIPanGestureRecognizer *)pan {

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

    CGPoint p = [pan translationInView:imageView];

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

    

    [pan setTranslation:CGPointZero inView:imageView];

    

}

 

 

#pragma  mark - 旋转

- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{

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

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

}

 

#pragma mark - 轻扫手势

- (void)swipeAction:(UISwipeGestureRecognizer *)swipe {

    

    if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {

        NSLog(@"123");

    }

    

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    self.view.backgroundColor = [UIColor whiteColor];

    

    

    // 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

 
 

//

//  ViewController.h

//  UI04_手势

//

//  Created by dllo on 16/1/8.

//  Copyright © 2016年 ZHAO. All rights reserved.

//

 

#import <UIKit/UIKit.h>

 

@interface ViewController : UIViewController

 

 

@end

 

//

//  ViewController.m

//  UI04_手势

//

//  Created by dllo on 16/1/8.

//  Copyright © 2016年 ZHAO. All rights reserved.

//

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

@end

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以参考以下步骤实现OC手势解锁demo: 1. 创建一个手势解锁的视图,可以使用`UIView`或`UIScrollView`等。 2. 在手势解锁视图中添加手势识别器,可以使用`UIPanGestureRecognizer`。 3. 在手势识别器回调函数中,记录手指的移动轨迹,可以使用`CGPoint`类型的数组。 4. 根据手指移动轨迹,在手势解锁视图上绘制手势轨迹,可以使用`UIBezierPath`绘制。 5. 根据手势轨迹判断手势是否正确,可以使用事先定义好的手势密码进行比对。 6. 根据比对结果,显示相应的提示信息。 以下是一个简单的OC手势解锁demo示例代码: ``` // 创建手势解锁视图 UIView *gestureView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)]; gestureView.center = self.view.center; gestureView.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:gestureView]; // 添加手势识别器 UIPanGestureRecognizer *gestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; [gestureView addGestureRecognizer:gestureRecognizer]; // 定义手势密码 NSArray *password = @[@(1), @(2), @(3), @(6), @(9)]; // 记录手指移动轨迹 NSMutableArray *path = [NSMutableArray array]; // 绘制手势轨迹 - (void)drawPath:(NSArray *)path { UIBezierPath *bezierPath = [UIBezierPath bezierPath]; for (int i = 0; i < path.count; i++) { CGPoint point = [path[i] CGPointValue]; if (i == 0) { [bezierPath moveToPoint:point]; } else { [bezierPath addLineToPoint:point]; } } CAShapeLayer *shapeLayer = [CAShapeLayer layer]; shapeLayer.path = bezierPath.CGPath; shapeLayer.strokeColor = [UIColor redColor].CGColor; shapeLayer.fillColor = [UIColor clearColor].CGColor; [self.view.layer addSublayer:shapeLayer]; } // 判断手势是否正确 - (BOOL)checkPassword:(NSArray *)path { if (path.count != password.count) { return NO; } for (int i = 0; i < path.count; i++) { if ([path[i] integerValue] != [password[i] integerValue]) { return NO; } } return YES; } // 手势识别器回调函数 - (void)handleGesture:(UIPanGestureRecognizer *)gestureRecognizer { CGPoint location = [gestureRecognizer locationInView:gestureView]; if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { [path removeAllObjects]; [path addObject:[NSValue valueWithCGPoint:location]]; } else if (gestureRecognizer.state == UIGestureRecognizerStateChanged) { [path addObject:[NSValue valueWithCGPoint:location]]; [self drawPath:path]; } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) { if ([self checkPassword:path]) { NSLog(@"密码正确"); } else { NSLog(@"密码错误"); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值