ios UIKit框架分析 第3天

来源:互联网 发布:bilibili手机直播软件 编辑:程序博客网 时间:2024/05/21 17:22

1.UIGestureRecognizer--关于ios的常见手势简述

-》父类

UIGestureRecognizer 是一个抽象类,是所有手势事件的父类。

The concrete subclasses of UIGestureRecognizer are the following:

UITapGestureRecognizer //轻拍
UIPinchGestureRecognizer //捏合
UIRotationGestureRecognizer //旋转
UISwipeGestureRecognizer //扫
UIPanGestureRecognizer//拖拽
UILongPressGestureRecognizer //长按


- (void)requireGestureRecognizerToFail:(UIGestureRecognizer *)otherGestureRecognizer;

手势共从的问题,otherGestureRecognizer实效时才执行当前手势-- 参考:http://shiminghua234.blog.163.com/blog/static/2639124220135842541889/


// 单击的 Recognizer    UITapGestureRecognizer* singleRecognizer;    singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleSingleTapFrom)];    singleTapRecognizer.numberOfTapsRequired = 1; // 单击    [self.view addGestureRecognizer:singleRecognizer];        // 双击的 Recognizer    UITapGestureRecognizer* double;    doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:selfaction:@selector(handleDoubleTapFrom)];    doubleTapRecognizer.numberOfTapsRequired = 2; // 双击    [self.view addGestureRecognizer:doubleRecognizer];        // 关键在这一行,如果双击确定偵測失败才會触发单击    [singleRecognizer requireGestureRecognizerToFail:doubleRecognizer];    [singleRecognizer release];    [doubleRecognizer release];
iOS7 -- 新特征

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizerNS_AVAILABLE_IOS(7_0);

返回值为yes,gestureRecognizer  让 otherGestureRecognizer实效。返回no,两者没有关联效果。

举例说明,当在app屏幕边缘的某个View pan时候,你可能希望这个view的所有子view的recognizer都失败,不再工作,否则可能会发生图像闪烁。
要达到这个目的,你可以这样来做

UIScreenEdgePanGestureRecognizer *myScreenEdgePanGestureRecognizer;    myScreenEdgePanGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc]                                        initWithTarget:self action:@selector(handleScreenEdgePan:)];    myScreenEdgePanGestureRecognizer.delegate = self;    // Configure the gesture recognizer and attach it to the view.
 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizershouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {    Appearance and Behavior    Using Gesture Recognizers    BOOL result = NO;    if ((gestureRecognizer == myScreenEdgePanGestureRecognizer) &&        [[otherGestureRecognizer view] isDescendantOfView:[gestureRecognizer view]]) {        result = YES;    }    return result;}


与上一个函数效果刚刚相反
返回值为yes,otherGestureRecognizer  让 gestureRecognizer 实效。返回no,两者没有关联效果。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldBeRequiredToFailByGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizerNS_AVAILABLE_IOS(7_0);



-》UITapGestureRecognizer

numberOfTapsRequired :设置轻击的次数,默认值为1

numberOfTouchesRequired :触点的数量,默认值为1,即手指数


-》UIPinchGestureRecognizer

当两个手指靠近表示zoom-in,当两个手指分离表示zoom-out。

@property(nonatomic) CGFloat scale,放大缩小因子

@property(nonatomic, readonly) CGFloat velocity ,只读属性 ,表示移动速度


-》UIRotationGestureRecognizer

rotation :旋转角度
velocity :速度


-》UILongPressGestureRecognizer

minimumPressDuration :长按最短的时间
numberOfTouchesRequired 触点的数量
numberOfTapsRequired 轻击的次数
allowableMovement :长按时运行移动的最大距离,默认值为10个像素


-> UIPanGestureRecognizer

maximumNumberOfTouches :最大触摸的数量
minimumNumberOfTouches :最少触摸的数量


- (CGPoint)translationInView:(UIView *)view

返回参数:该方法返回在横坐标上、纵坐标上拖动了多少像素  x方向:向右point.x 为正,向左为负,y轴同x轴;


- (void)setTranslation:(CGPoint)translation inView:(UIView *)view;

 self.imageView  = [[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 30, 30)];    self.imageView.image = [UIImage imageNamed:@"10yuan"];        UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];    [self.imageView addGestureRecognizer:panGesture];    [self.imageView setUserInteractionEnabled:YES];        [self.view addSubview:self.imageView];


因为拖动起来一直是在递增,所以每次都要用setTranslation:方法制0这样才不至于不受控制般滑动出视图
-(void)handlePan:(UIPanGestureRecognizer*)pan{    CGPoint point = [pan translationInView:self.view];    NSLog(@"%f,%f",point.x,point.y);    pan.view.center = CGPointMake(pan.view.center.x + point.x, pan.view.center.y + point.y);    [pan setTranslation:CGPointMake(0, 0) inView:self.view];}


(手势的速度,这是表示在每分每秒。速度分解为水平和垂直分量。)

- (CGPoint)velocityInView:(UIView *)view


-》UISwipeGestureRecognizer

direction 扫动方向,默认值UISwipeGestureRecognizerDirectionRight

typedef enum {
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
} UISwipeGestureRecognizerDirection;

numberOfTouchesRequired : 触点的数量,默认值为1,即手指数


2.UIMenuController--自定义弹出菜单

-》弹出菜单的视图必须成为第一响应者

-(BOOL)canBecomeFirstResponder{    return YES;}
-(void)action{    if([self becomeFirstResponder]){        NSLog(@"yes");    }    UIMenuItem *flag = [[UIMenuItem alloc] initWithTitle:@"Flag"action:@selector(flag:)];    UIMenuItem *approve = [[UIMenuItem alloc] initWithTitle:@"Approve"action:@selector(approve:)];    UIMenuItem *deny = [[UIMenuItem alloc] initWithTitle:@"Deny"action:@selector(deny:)];    UIMenuController *menu = [UIMenuController sharedMenuController];    [menu setMenuItems:[NSArray arrayWithObjects:flag, approve, deny, nil]];    [menu setTargetRect:_btn.frame inView:self.view];    [menu setMenuVisible:YES animated:YES];    [menu setArrowDirection:UIMenuControllerArrowRight];}- (void)flag:(id)sender {    NSLog(@"Cell was flagged");}- (void)approve:(id)sender {    NSLog(@"Cell was approved");}- (void)deny:(id)sender {    NSLog(@"Cell was denied");}


3.UIInterpolatingMotionEffect--ios7.0

使物体随着设备移动而移动

- (void)viewDidLoad {    [super viewDidLoad];    [self registerEffectForView:_btn depth:100.0f];}- (void)registerEffectForView:(UIView*)_effectView depth:(CGFloat)_depthF{            effectX = [[[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis]retain];        effectY = [[[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis]retain];        effectX.maximumRelativeValue = @(_depthF);        effectX.minimumRelativeValue = @(-_depthF);        effectY.maximumRelativeValue = @(_depthF);        effectY.minimumRelativeValue = @(-_depthF);        [_effectView addMotionEffect:effectX];        [_effectView addMotionEffect:effectY];}




0 0
原创粉丝点击