English 中文(简体)
移动手柄 第一次触摸事件iOS
原标题:touchesMoved handle first touch event iOS

我用这个方法将我的东西移到视野中:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

我用这个方法写了下一封信:

NSLog(@"event: %@", event);

并获得此结果 :

event: <UITouchesEvent: 0x310d00> timestamp: 5277.16 touches: {(
    <UITouch: 0x3f1110> phase: Moved tap count: 1 window: <UIWindow: 0x3117d0; frame = (0 0; 768 1024); layer = <UIWindowLayer: 0x30cb20>> view: <UIView: 0x330520; frame = (0 0; 768 1024); transform = [0, -1, 1, 0, 0, 0]; autoresize = W+H; layer = <CALayer: 0x330550>> location in window: {279, 677} previous location in window: {305, 694} location in view: {347, 279} previous location in view: {330, 305},
    <UITouch: 0x4a1a710> phase: Stationary tap count: 1 window: <UIWindow: 0x3117d0; frame = (0 0; 768 1024); layer = <UIWindowLayer: 0x30cb20>> view: (null) location in window: {412, 89} previous location in window: {413, 89} location in view: {412, 89} previous location in view: {413, 89}
)}

我可以看到我所有的触摸(目前只有两只触摸),

我的问题是如何接触?

这需要移动我的东西。

现在我要用这个:

UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];

但当我移动我的东西时,我得到所有触摸的价值。如何处理只是第一次触摸?

非常感谢!

最佳回答

你这样做的方式没有错 但如果你真的想知道 如何得到第一个对象,它就来了

参数接触为 NSSet 类型。如果您查看 < a href=> http:// developer.apple.com/library/ios/#DOCUATION/Cocoa/Reference/Foundation/Classes/NSSet_Classes/Reference/Reference.html" rel="nofollow" >NSSet Set类参考 < ahref=""http://developer.apple.com/library/ios/DOCUMATION/Cocoa/Reference/Foundation/Class/NSet_Class/Reference/Reference.html#/ apple_ref/doc/uid/uid/20000143-CHDFHBE" rel="nofol"\code> - (NSAray*) allObjects <

这样您就可以通过下列方式获得第一次触摸 :

UITouch *touch = (UITouch *)[[[event allTouches] allObjects] objectAtIndex: 0];

并查看"http:// developmenter.apple.com/library/ios/#documentation/uikit/referation/UIEvent_Class/Reference/Rel=“nofollow”>UIEvent class reference 。这里你会发现,所有Touches并不是唯一的方法。类也懂得如何返回:

- (NSSet *)touchesForGestureRecognizer:(UIGestureRecognizer *)gesture

- (NSSet *)touchesForView:(UIView *)view

- (NSSet *)touchesForWindow:(UIWindow *)window

所有这些方法都返回 NSSet 。 您可以使用 NSSet s allObjects 方法, 将 转换为 NSArray

问题回答

如果您正在使用触摸事件来拖动视图,可以使用 UIPanGestureRecognizer 。

识别器的属性类似最小/最大触摸次数, 由苹果制作, 正好用于拖动视图 。

UIPanGesturrecureRegnizer 分类参考





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...