1.UIGestureRecognizer--关于ios的常见手势简述
UIGestureRecognizer 是一个抽象类,是所有手势事件的父类。
The concrete subclasses of UIGestureRecognizer are the following:
UITapGestureRecognizer
UIPinchGestureRecognizer
UIRotationGestureRecogni
UISwipeGestureRecognizer
UIPanGestureRecognizer//拖拽
UILongPressGestureRecogn
- (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 *)gestureRecognizer
shouldBeRequiredToFailByGestureRecognizer:(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
-》UIPinchGestureRecognizer
@property(nonatomic) CGFloat scale,放大缩小因子
@property(nonatomic, readonly) CGFloat velocity
-》UIRotationGestureRecogni
-》UILongPressGestureRecogn
-> UIPanGestureRecognizer
- (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];
-(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
typedef enum {
} UISwipeGestureRecognizer
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];
}