Có cách nào để nhận các đối tượng UITouch
được liên kết với cử chỉ không? UIGestureRecognizer
dường như không có bất kỳ phương pháp nào cho việc này.Nhận các đối tượng UITouch cho thiết bị nhận dạng UIGestureRecognizer
Trả lời
Quyền của Jay ... bạn sẽ muốn có một phân lớp. Hãy thử cái này cho kích thước, nó là từ một trong những dự án của tôi. Trong 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
Và trong 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
Tất nhiên, bạn sẽ cần phải thực hiện các phương pháp
- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event;
trong đại biểu của bạn - ví dụ :
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];
}
Nếu bạn đang viết UIGestureRecognizer riêng của bạn, bạn có thể nhận được các đối tượng liên lạc trọng:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
hoặc tương đương di chuyển, kết thúc hoặc hủy
Các Apple docs có rất nhiều thông tin về subclassing
Nếu bạn chỉ cần tìm ra vị trí của cử chỉ, bạn có thể gọi vị tríInView: hoặc locationOfTouch: inView: trên đối tượng UIGestureRecognizer. Tuy nhiên nếu bạn muốn làm bất cứ điều gì khác, sau đó bạn sẽ cần phải phân lớp.
Nếu đó chỉ là vị trí bạn quan tâm, bạn không phải phân lớp, bạn sẽ được thông báo về những thay đổi về vị trí của vòi từ trình nhận dạng UIGestureRecognizer.
Initialize với mục tiêu:
self.longPressGestureRecognizer = [[[UILongPressGestureRecognizer alloc] initWithTarget: self action: @selector(handleGesture:)] autorelease];
[self.tableView addGestureRecognizer: longPressGestureRecognizer];
Xử lý UIGestureRecognizerStateChanged để có được những thay đổi vị trí:
- (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;
}
}
này doesn' t nhận được 'UITouch', nó chỉ nhận được một' CGPoint' cho khung cục bộ của khung nhìn cụ thể. – Chris
Dưới đây là một phương pháp để có được một nền báo chí dài thêm vào một UIView tùy ý.
Điều này cho phép bạn chạy longpress trên bất kỳ số lượng UIView nào. Trong ví dụ này, tôi hạn chế quyền truy cập vào phương thức UIView thông qua các thẻ nhưng bạn cũng có thể sử dụng isKindOfClass.
(Từ những gì tôi tìm thấy bạn phải sử dụng touchesMoved cho nhấn và giữ vì touchesBegan cháy trước khi nhấn và giữ sẽ được kích hoạt)
lớp con - .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
lớp con - .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 -.m
#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
}
}
đơn giản và nhanh chóng:
NSArray *touches = [recognizer valueForKey:@"touches"];
đâu recognizer là bạn UIGestureRecognizer
iOS 8 [
- 1. Đối tượng được phân giải nhận thông báo nhận dạng UIGestureRecognizer
- 2. Cách lấy vị trí UITouch từ UIGestureRecognizer
- 3. Nghe sự kiện UITouch cùng với UIGestureRecognizer
- 4. Nhận dạng đối tượng R
- 5. lập trình nhận dạng thiết bị iphone
- 6. Nhận dạng đối tượng đơn giản
- 7. Phân biệt giữa các đối tượng UITouch trên iPhone
- 8. Nhận thiết bị IMEI
- 9. Chấp nhận tham số dưới dạng đối tượng riêng lẻ hoặc dưới dạng mảng đối tượng
- 10. Truy lục số nhận dạng thiết bị iOS
- 11. Eclipse không nhận dạng được thiết bị Nexus One
- 12. có thể nhận số nhận dạng duy nhất từ thiết bị di động không?
- 13. Nhận dạng đối tượng và kích thước đo
- 14. Bệnh có tên là nhận dạng đối tượng
- 15. Nhận các phương thức đối tượng R
- 16. Đọc giá trị nhận dạng đối tượng Hibernate
- 17. Mạo danh nhận dạng xác nhận quyền sở hữu đối với cửa sổ nhận dạng
- 18. Nhận đối tượng JQuery trống
- 19. Đối tượng được chỉ định không được nhận dạng là đối tượng giả. Vấn đề
- 20. Nhận mục/đối tượng trong đó phần tử bị xóa
- 21. DBCC CHECKIDENT Thiết lập nhận dạng 0
- 22. cách nhận mã thông báo thiết bị?
- 23. Nhận tệp NSUserDefaults plist từ thiết bị
- 24. Nhận hướng của thiết bị Android
- 25. Đề xuất cho thuật toán nhận dạng đối tượng sử dụng thông tin chuyên sâu
- 26. Nhận đối tượng theo id()?
- 27. VSIX: Nhận đối tượng DTE
- 28. Cách nhận tên thiết bị thân thiện từ DEV_BROADCAST_DEVICEINTERFACE và ID thiết bị thiết bị
- 29. Nhận hộp giới hạn "hiển thị" cho đối tượng bị cắt
- 30. Làm cách nào tôi có thể yêu cầu một Trình nhận dạng UIGestureRecognizer hủy một liên lạc hiện có?
Đừng quên thêm #import để DragGestureRecognizer.h nộp –
HotJard