有没有办法让UITouch
对象与手势关联? UIGestureRecognizer
似乎没有任何方法。获取UITestureRecognizer的UITouch对象
回答
的Apple docs有大量的信息
杰伊的权利...你会想要一个子类。试试这个大小,它来自我的一个项目。在DragGestureRecognizer.h:
@interface DragGestureRecognizer : UILongPressGestureRecognizer {
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
@end
@protocol DragGestureRecognizerDelegate <UIGestureRecognizerDelegate>
- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event;
@end
而且在DragGestureRecognizer.m:
#import "DragGestureRecognizer.h"
@implementation DragGestureRecognizer
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
if ([self.delegate respondsToSelector:@selector(gestureRecognizer:movedWithTouches:andEvent:)]) {
[(id)self.delegate gestureRecognizer:self movedWithTouches:touches andEvent:event];
}
}
@end
当然,你需要实现
- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event;
方法在委托 - 例如:
DragGestureRecognizer * gr = [[DragGestureRecognizer alloc] initWithTarget:self action:@selector(pressed:)];
gr.minimumPressDuration = 0.15;
gr.delegate = self;
[self.view addGestureRecognizer:gr];
[gr release];
- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event{
UITouch * touch = [touches anyObject];
self.mTouchPoint = [touch locationInView:self.view];
self.mFingerCount = [touches count];
}
如果你正在写自己的UIGestureRecognizer你可以触摸的物体覆盖:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
或等效的移动,结束或继承
如果您只需要查找手势的位置,则可以调用UIGestureRecognizer对象上的locationInView:或locationOfTouch:inView:。但是如果你想做其他事情,那么你需要继承子类。
如果它只是您感兴趣的位置,您不需要继承子类,就会收到来自UIGestureRecognizer的位置更改通知。
初始化目标:
self.longPressGestureRecognizer = [[[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleGesture:)] autorelease];
[self.tableView addGestureRecognizer: longPressGestureRecognizer];
手柄UIGestureRecognizerStateChanged获得位置的变化:
- (void)handleGesture: (UIGestureRecognizer *)theGestureRecognizer {
switch (theGestureRecognizer.state) {
case UIGestureRecognizerStateBegan:
case UIGestureRecognizerStateChanged:
{
CGPoint location = [theGestureRecognizer locationInView: self.tableView];
[self infoForLocation: location];
break;
}
case UIGestureRecognizerStateEnded:
{
NSLog(@"Ended");
break;
}
default:
break;
}
}
这没有按” t获得'UITouch',它只为特定视图的本地帧获取'CGPoint'。 – Chris 2014-12-14 19:58:27
这里得到一个长按添加到任意的UIView的方法。
这可让您在任意数量的UIView上运行longpress。 在这个例子中,我通过标签限制对UIView方法的访问,但也可以使用isKindOfClass。
(从我发现你必须使用touchesMoved为长按,因为火灾的touchesBegan长按该变活动前)
子类 - .H
#import <UIKit/UIKit.h>
#import <UIKit/UIGestureRecognizerSubclass.h>
@interface MyLongPressGestureRecognizer : UILongPressGestureRecognizer
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
@property (nonatomic) CGPoint touchPoint;
@property (strong, nonatomic) UIView* touchView;
@end
子类 - 的.m
#import "MyLongPress.h"
@implementation MyLongPressGestureRecognizer
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
{
UITouch * touch = [touches anyObject];
self.touchPoint = [touch locationInView:self.view];
self.touchView = [self.view hitTest:[touch locationInView:self.view] withEvent:event];
}
#pragma mark - Setters
-(void) setTouchPoint:(CGPoint)touchPoint
{
_touchPoint =touchPoint;
}
-(void) setTouchView:(UIView*)touchView
{
_touchView=touchView;
}
@end
viewcontroller - .h
//nothing special here
viewcontroller - 。米
#import "ViewController.h"
//LOAD
#import "MyLongPress.h"
@interface ViewController()
//lOAD
@property (strong, nonatomic) MyLongPressGestureRecognizer* longPressGesture;
@end
@implementation PDViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//LOAD
self.longPressGesture =[[MyLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)];
self.longPressGesture.minimumPressDuration = 1.2;
[[self view] addGestureRecognizer:self.longPressGesture];
}
//LOAD
-(void) setLongPressGesture:(MyLongPressGestureRecognizer *)longPressGesture {
_longPressGesture = longPressGesture;
}
- (void)longPressHandler:(MyLongPressGestureRecognizer *)recognizer {
if (self.longPressGesture.touchView.tag >= 100) /* arbitrary method for limiting tap */
{
//code goes here
}
}
简单快捷:
NSArray *touches = [recognizer valueForKey:@"touches"];
识别在哪里你UIGestureRecognizer
iOS 8 [
- 1. 检测对象,touchesmoved和UITouch
- 2. UITouch对象行为古怪
- 3. Objective-C:获取UIEvent触发的UITouch
- 4. 我是否需要释放UITouch对象?
- 5. 使用UITouch检测特定对象
- 6. 获取对象
- 7. 获取对象
- 8. 获取对象
- 9. 获取对象
- 10. 获取对象
- 11. 获取对象
- 12. 获取对象的对象数组
- 13. Hibernate获取链接对象的对象
- 14. 如何获取对象内的对象?
- 15. C#获取通用对象的对象
- 16. 从对象获取对象的键值
- 17. 获取对象的DataContext的
- 18. 获取onActivityResult对象
- 19. 获取对象8
- 20. restfb获取对象
- 21. 获取从对象
- 22. 获取nasted对象
- 23. 获取JavaScript对象
- 24. spymemcached:获取对象
- 25. 获取JavaScript对象
- 26. 获取从对象
- 27. 获取对象值
- 28. 获取从对象
- 29. 获取从对象
- 30. 获取父对象
不要忘记添加#进口到DragGestureRecognizer.h文件 –
HotJard
2013-02-17 14:21:22