有沒有辦法讓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. 從UITouch對象調用UIGestureRecognizer
- 2. UITouch對象行爲古怪
- 3. 檢測對象,touchesmoved和UITouch
- 4. 無法從UIButton的獲取UITouch事件
- 5. Objective-C:獲取UIEvent觸發的UITouch
- 6. 我是否需要釋放UITouch對象?
- 7. 使用UITouch檢測特定對象
- 8. 獲取對象
- 9. 獲取對象
- 10. 獲取對象
- 11. 獲取對象
- 12. 獲取對象
- 13. 獲取對象
- 14. 獲取對象
- 15. 獲取對象
- 16. 獲取[對象XrayWrapper [對象HTMLDivElement]]對象
- 17. 獲取對象對象內的數據?
- 18. 從對象獲取對象的鍵值
- 19. 獲取對象的對象數組
- 20. Hibernate獲取鏈接對象的對象
- 21. 如何獲取對象內的對象?
- 22. C#獲取通用對象的對象
- 23. 獲取對象的DataContext的
- 24. 獲取從對象
- 25. 獲取從對象
- 26. restfb獲取對象
- 27. 獲取JavaScript對象
- 28. 獲取nasted對象
- 29. 獲取從對象
- 30. 獲取對象8
不要忘記添加#進口到DragGestureRecognizer.h文件 –
HotJard
2013-02-17 14:21:22