UI之gestureRecognizer

本文详细介绍了一个iOS应用中如何实现各种手势识别,包括轻拍、长按、旋转、捏合、平移及轻扫等手势,并提供了具体的代码实现案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

屏幕的各种手势识别

#import "MangoView.h"
@interface MangoView()
@property(nonatomic,retain)UIImageView *rotationImageView;
@end
@implementation MangoView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self loadingCustomView];
    }
    return self;
}
- (void)dealloc{
    [self.rotationImageView release];
    [super dealloc];
}
- (void)loadingCustomView{
    self.backgroundColor = [UIColor orangeColor];
  //轻拍手势
    //初始化创建一个轻拍手势,并设置target action
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    //点击次数
    tap.numberOfTapsRequired = 1;
    //手指个数
    tap.numberOfTouchesRequired = 1;
    //将手势添加到视图上
    [self addGestureRecognizer:tap];
    [tap release];
    
    //长按手势
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
    //长按时间
    longPress.minimumPressDuration = 0.1;
    //点击次数
    longPress.numberOfTapsRequired = 1;
    //longPress.numberOfTouchesRequired = 1;
    [self addGestureRecognizer:longPress];
    [longPress release];
    
    //创建一个UIImageView+image相框加相片
    self.rotationImageView = [[UIImageView alloc]initWithFrame:self.frame];
    self.rotationImageView.image = [UIImage imageNamed:@"1.png"];
    //打开imageView的用户交互
    self.rotationImageView.userInteractionEnabled = YES;
    [self addSubview:self.rotationImageView];
    [self.rotationImageView release];
    
   //旋转手势
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
    [self.rotationImageView addGestureRecognizer:rotation];
    [rotation release];
    
    //捏合手势
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
    [self addGestureRecognizer:pinch];
    [pinch release];
    
    //平移手势
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [self.rotationImageView addGestureRecognizer:pan];
    [pan release];
    
    //轻扫
    //只能设置水平或者竖直方向的轻扫,不可同时设置
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
    //设置方向,水平
    swipe.direction = UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft;
    [self.rotationImageView addGestureRecognizer:swipe];
    [swipe release];
    UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe1Action:)];
    //设置方向,竖直
    swipe1.direction = UISwipeGestureRecognizerDirectionUp|UISwipeGestureRecognizerDirectionDown;
    [self.rotationImageView addGestureRecognizer:swipe1];
    [swipe1 release];
    
    //从屏幕边缘轻扫
    UIScreenEdgePanGestureRecognizer *screen = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screenAction:)];
    //只能设置一个方向的边缘轻扫
    screen.edges = UIRectEdgeLeft;
    [self.rotationImageView addGestureRecognizer:screen];
    [screen release];
    
}
- (void)screenAction:(UIScreenEdgePanGestureRecognizer *)screeen{
    NSLog(@"从屏幕边缘轻扫");

}
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe{
    NSLog(@"水平方向清扫");

}
- (void)swipe1Action:(UISwipeGestureRecognizer *)swipe1{
    NSLog(@"竖直方向清扫");
    
}
- (void)rotation:(UIRotationGestureRecognizer *)ro{
    //transform形变
    //第一个参数是视图原来的形变状态,旋转手势的旋转弧度
    self.rotationImageView.transform = CGAffineTransformRotate(self.rotationImageView.transform, ro.rotation);
    NSLog(@"%f",ro.rotation);
    //重置旋转角度,如果不重置那么每次的旋转都是基于原来的基础上旋转的(假如上次旋转到四十五°的位置,下一个点就是46°,如果不重置为0,那么下次的形变就是从45°的位置开始旋转46°,一下就到了91°,跨度范围越来越大,导致图片旋转越来越快)
    ro.rotation = 0;
 }
- (void)panAction:(UIPanGestureRecognizer *)pan{
    CGPoint offset = [pan translationInView:self.rotationImageView];
    self.rotationImageView.transform = CGAffineTransformTranslate(self.rotationImageView.transform, offset.x, offset.y);
    //每次相对于0.0平移
    [pan setTranslation:CGPointZero inView:self.rotationImageView];
}
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
    //捏合手势有一个属性scale就是捏合的比例
    //第一个参数是上次形变的位置
    self.rotationImageView.transform = CGAffineTransformScale(self.rotationImageView.transform, pinch.scale, pinch.scale);
    NSLog(@"%f",pinch.scale);
    //把缩放比例重置为1.0,每次放大和缩小的比例都基于1.0
    pinch.scale = 1.0;
}
- (void)tapAction{
    NSLog(@"1根手指点击一次");
}
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress{
    //长按手势默认执行两次,长按开始和手指离开都触发
    if (longPress.state  == UIGestureRecognizerStateBegan) {
         NSLog(@"*****");
    }else if (longPress.state == UIGestureRecognizerStateEnded){
        NSLog(@"@@@@@");
    }
}
@end


#import "MangoViewController.h"
#import "MangoView.h"
@interface MangoViewController ()

@end

@implementation MangoViewController
- (void)loadView{
    [super loadView];
    self.view = [[MangoView alloc] initWithFrame:self.view.frame];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

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

### Behavior Mono in UI Development In the context of using Mono for UI development, particularly with platforms like iOS and Android through frameworks such as Xamarin (formerly known as MonoTouch), behaviors play a significant role in enhancing user interface interactivity[^1]. Behaviors allow developers to encapsulate specific actions or animations that can be triggered by events within the application's lifecycle. #### Defining Behaviors Behaviors are reusable pieces of code designed to modify or extend the functionality of existing controls without altering their core logic. In Xamarin.Forms, which builds upon Mono’s capabilities, behaviors provide an elegant way to attach event handlers directly from XAML or C#. This approach simplifies complex interactions while maintaining clean separation between view models and views. For instance, consider implementing a simple behavior that changes text color when tapped: ```csharp public class TapToChangeTextColorBehavior : Behavior<Label> { protected override void OnAttachedTo(Label bindable) { base.OnAttachedTo(bindable); var gestureRecognizer = new TapGestureRecognizer(); gestureRecognizer.Tapped += HandleTap; bindable.GestureRecognizers.Add(gestureRecognizer); } private void HandleTap(object sender, EventArgs e) { ((Label)sender).Textcolor = Color.Red; // Change TextColor property here. } } ``` This example demonstrates attaching custom tap recognition to labels dynamically via behaviors rather than hardcoding this interaction inside each page where it might appear multiple times across various screens. #### Common Issues Encountered When Using Behaviors One common challenge faced during implementation involves ensuring proper detachment of gestures once elements go out of scope. Failure to do so may lead to memory leaks since references remain active even after removal from visual hierarchy. To mitigate this risk, always ensure `OnDetachingFrom` method is implemented correctly alongside its counterpart attachment handler. Another issue pertains to cross-platform compatibility concerns especially given differences among operating systems regarding touch input handling mechanisms. Developers must test thoroughly under diverse conditions including varying screen sizes/resolutions found on modern devices[^2].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值