长按、捏合、旋转手势的组合应用及ios8新特性

88 篇文章 0 订阅

技术更新换代更快 为了给新手学习

这里总结了一些手势 和ios 8 的一些新特性

封装了公共类 可供大家随意使用 便捷我们项目的开发 

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface Utility : NSObject

/**
 *  获取系统版本
 *
 *  @return <#return value description#>
 */
+ (CGFloat)systemVersion;

/**
 *  显示alertView
 *
 *  @param message <#message description#>
 */
+ (void)showAlertViewWithMessage:(NSString *)message;

/**
 *  显示alertController(8.0)
 *
 *  @param message <#message description#>
 */
+ (void)showAlertViewWithMessage:(NSString *)message viewController:(UIViewController *)viewController;
@end

#import "Utility.h"

@implementation Utility

/**
 *  获取系统版本
 *
 *  @return <#return value description#>
 */
+ (CGFloat)systemVersion
{
    return [UIDevice currentDevice].systemVersion.floatValue;
}

/**
 *  显示alertView
 *
 *  @param message <#message description#>
 */
+ (void)showAlertViewWithMessage:(NSString *)message
{
    //警告框
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
    [alertView show];
}

/**
 *  显示alertController(8.0)
 *
 *  @param message <#message description#>
 */
+ (void)showAlertViewWithMessage:(NSString *)message viewController:(UIViewController *)viewController
{
    /*
     UIAlertController是UIAlertView和UIActionSheet的结合体
     */
    
    //警告框
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    
    //动作
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        //点击按钮触发事件
        NSLog(@"点击取消按钮");
    }];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"点击确定按钮");
        //取得输入框对象
        NSArray *array = [alertController textFields];
        
        UITextField *userTf = array[0];
        UITextField *pwdTf = array[1];
        
        //
        NSLog(@"---%@---%@",userTf.text,pwdTf.text);
    }];
    
    //添加动作
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    
    //添加输入框
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.textColor = [UIColor redColor];
        textField.placeholder = @"请输入用户名";
       
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"请输入密码";
        //安全输入
        textField.secureTextEntry = YES;
    }];
    
    //显示
    [viewController presentViewController:alertController animated:YES completion:nil];
}

@end

#import "ViewController.h"
#import "Uitility.h"

@interface ViewController ()<UIGestureRecognizerDelegate>

{
    CGFloat _lastRotation;
}

@property (nonatomic, weak)UIImageView *imageView;


@end

@implementation ViewController

- (UIImageView *)imageView
{
    
    if (!_imageView) {
      
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d",1]]];
        imageView.frame = CGRectMake(0,0 , 100, 100);
        imageView.center = self.view.center;
       
        //开启用户交互
        imageView.userInteractionEnabled = YES;
        [self.view addSubview:imageView];
        _imageView = imageView;
            
        
    }
    return _imageView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addRotationGesture];
    
    [self addPinchGesture];
    
    [self addLongPressGesture];
    
    [self imageView];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //获取用户名和密码输入框
    UITextField *usernameTf = [alertView textFieldAtIndex:0];
    UITextField *passwordTf = [alertView textFieldAtIndex:1];
    NSLog(@"%@--- %@",usernameTf.text,passwordTf.text);
}


/**
 *  添加长按手势
 *
 *  @return <#return value description#>
 */
- (void)addLongPressGesture
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandle:)];
    //从手指按下开始计时,过了0.5秒会调用longPressHandle:
    longPress.minimumPressDuration = 0.5;
    longPress.delegate = self;
    [self.imageView addGestureRecognizer:longPress];
}

- (void)longPressHandle:(UILongPressGestureRecognizer *)longPress
{
    if(longPress.state == UIGestureRecognizerStateBegan)
    {
        NSLog(@"长按开始");
        
        if ([Uitility systemVersion] >= 8.0) {
            [Uitility showAlertViewMessage:@"长按图片" viewController:self];
        }
        else
        {
            [Uitility showAlertViewMessage:@"长按图片"];
        }
        
    }
    else if(longPress.state == UIGestureRecognizerStateEnded)
    {
        NSLog(@"长按结束");
    }
}
//添加捏合手势
- (void)addPinchGesture
{
    UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchHandle:)];
    pin.delegate = self;
    [self.imageView addGestureRecognizer:pin];
}

/**
 *  捏合手势
 *
 *  @return
 */
- (void)pinchHandle:(UIPinchGestureRecognizer *)pinch
{
    //比例
    CGFloat scale = pinch.scale;
    
    //修改视图比例
    self.imageView.transform = CGAffineTransformScale(self.imageView.transform, scale, scale);
    
    //清除叠加的数据
    pinch.scale = 1.0;
}


//添加选装手势
- (void)addRotationGesture
{
    UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
    rotationGesture.delegate = self;
    
        [self.imageView addGestureRecognizer:rotationGesture];

    
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    
    UITouch *touch = [touches anyObject];
    
    CGPoint center = [touch locationInView:self.view];
    
    self.imageView.center = center;
    
}
- (void)rotation:(UIRotationGestureRecognizer *)rotationGesture
{
    
    //获取旋转弧度
    CGFloat progress = rotationGesture.rotation;
    
    //修改图片的弧度
    self.imageView.transform = CGAffineTransformMakeRotation(_lastRotation + progress);
    
    if (rotationGesture.state == UIGestureRecognizerStateEnded) {
        _lastRotation += progress;
    }
    
}

/**
 *  多个手势同时进行
 *
 *  @param gestureRecognizer
 *  @param otherGestureRecognizer
 *
 *  @return
 */
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return YES;
}

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

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值