English 中文(简体)
any 在任何角度的检测
原标题:Swipe detection in any angle

Is there any way I can detect swipe in iPhone in any angle? UISwipeGestureRecognizer seems to have only 4 directions.
If I swipe like this:


 
  
   X

I want it to give me something like 60 degrees and not just down like the UISwipeGestureRecognizer.
How can I do this?

问题回答

页: 1 当你发现最终状态时,你能够迅速。 速度分为x和 y部分。 您可使用X和x部分计算坡度、米。

m = ∆y / ∆x

斜坡、米和轴线的斜线,由以下定义:

? = 弧度

类似:

- (void)didPan:(UIPanGestureRecognizer*)recognizer {
    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan:
            ...
            break;

        case UIGestureRecognizerStateEnded:
            CGPoint velocity = [recognizer velocityInView:[recognizer.view superview]];
            // If needed: CGFloat slope = velocity.y / velocity.x;
            CGFloat angle = atan2f(velocity.y, velocity.x);
            ...
            break;
    }
}

你只能发现接触点的开始和停止,并计算两点的视角。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//global CGPoint.
    //this should be it s GLOBAL coordinates, not just relative to the view    
    startPoint=[[touches anyObject] locationInView:self.superview.superview];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    //global CGPoint
    endPoint=[[touches anyObject] locationInView:self.superview.superview];
}

计算他们之间的争 the,你可以这样作:

static inline CGFloat angleBetweenLinesInRadians(CGPoint line1Start, CGPoint line1End, CGPoint line2Start, CGPoint line2End) {

    CGFloat a = line1End.x - line1Start.x;
    CGFloat b = line1End.y - line1Start.y;
    CGFloat c = line2End.x - line2Start.x;
    CGFloat d = line2End.y - line2Start.y;

    CGFloat line1Slope = (line1End.y - line1Start.y) / (line1End.x - line1Start.x);
    CGFloat line2Slope = (line2End.y - line2Start.y) / (line2End.x - line2Start.x);

    CGFloat degs = acosf(((a*c) + (b*d)) / ((sqrt(a*a + b*b)) * (sqrt(c*c + d*d))));
    return (line2Slope > line1Slope) ? degs : -degs;    
}
//This code came from someone else and I don t remember who to give credit to.

因此,可以找到一条横向线的角,这样你就可以做一些事情。

CGFloat angle=angleBetweenLinesInRadians(startPoint, endPoint, startPoint, CGPointMake(startPoint.x + 10, startPoint.y));

这将是这样一种观点。

________
 this angle
 
  
   x

希望这一帮助

<><>><>>>>>>><<>>>>>>>> 更好的方式

你们可以做的是下级的UIGesture Recognizer

#import <UIKit/UIGestureRecognizerSubclass.h>

之后,你执行这些方法。

- (void)reset;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

其中每一种财产都用于确定和确定国家财产用于该姿态。

There is a full example here: http://developer.apple.com/library/ios/#documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/GestureRecognizers/GestureRecognizers.html





相关问题
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, ...

热门标签