代码之家  ›  专栏  ›  技术社区  ›  mootymoots

在UITextView中成功刷卡?

  •  4
  • mootymoots  · 技术社区  · 15 年前

    我的UITextView已禁用滚动-除非有其他方法实现多行文本?

    6 回复  |  直到 15 年前
        1
  •  3
  •   Ross    15 年前

    这是UITextView的一个子类,它将检测一个刷卡手势。。。

    这就是你要找的吗?

    #import <UIKit/UIKit.h>
    
    #define kMinimumGestureLength   25
    #define kMaximumVariance        5
    
    @interface SwipeableTextView : UITextView {
        CGPoint gestureStartPoint;
    }
    
    @end
    
    @implementation SwipeableTextView
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesBegan:touches withEvent:event];
    
        UITouch *touch =[touches anyObject];
        gestureStartPoint = [touch locationInView:self];
    
    }
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesMoved:touches withEvent:event];
    
        UITouch *touch = [touches anyObject];
        CGPoint currentPosition = [touch locationInView:self];
    
        CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
        CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);
    
        if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance) {
            NSLog(@"Horizontal swipe detected");
        }
    
    }
    
    @end
    
        2
  •  3
  •   Jason    15 年前

    我发现,一个更好的解决方案是简单地将触摸传递回superview:

    @interface SwipeableTextView : UITextView {
    }
    
    @end
    
    @implementation SwipeableTextView
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesBegan:touches withEvent:event];
    
        [self.superview touchesBegan:touches withEvent:event];
    
    }
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesMoved:touches withEvent:event];
    
        [self.superview touchesMoved:touches withEvent:event];
    }
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesEnded:touches withEvent:event];
    
        [self.superview touchesEnded:touches withEvent:event];
    } 
    
    @end
    
        3
  •  2
  •   kennytm    15 年前

    添加透明 UIView UITextView 用它来处理刷卡,然后发送 touchesBegan/Moved/Ended/Canceled:

        4
  •  2
  •   Cliff McC    15 年前

    我拿了罗斯的代码,添加了一些帮助我的东西。特别是,此代码在停止之前不会响应刷卡操作。它还可以防止刷卡中途反转方向。

    #import <UIKit/UIKit.h>
    
    #define kMinimumGestureLength   25
    #define kMaximumVariance        5
    
    typedef enum swipeDirection {
        kSwipeNone,
        kSwipeLeft,
        kSwipeRight
    } tSwipeDirection;
    
    @interface SwipeableTextView : UITextView {
        CGPoint gestureStartPoint;
        tSwipeDirection swipeDirection;
    }
    
    @end
    
    @implementation SwipeableTextView
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesBegan:touches withEvent:event];
    
        swipeDirection = kSwipeNone;
        UITouch *touch =[touches anyObject];
        gestureStartPoint = [touch locationInView:self];
    
    }
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesMoved:touches withEvent:event];
    
        UITouch *touch = [touches anyObject];
        CGPoint currentPosition = [touch locationInView:self];
    
        CGFloat deltaX = fabsf(gestureStartPoint.x - currentPosition.x);
        CGFloat deltaY = fabsf(gestureStartPoint.y - currentPosition.y);
    
        // Check if we already started a swipe in a particular direction
        // Don't let the user reverse once they get going
        if (deltaX >= kMinimumGestureLength && deltaY <= kMaximumVariance &&
            swipeDirection == kSwipeNone) {
            if (gestureStartPoint.x < currentPosition.x) {
                swipeDirection = kSwipeRight;
            }
            else {
                swipeDirection = kSwipeLeft;
            }
        }
    }
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        if (swipeDirection == kSwipeRight) {
            NSLog(@"Swipe right");
        }
        else if (swipeDirection == kSwipeLeft) {
            NSLog(@"Swipe left");
        }
        [super touchesEnded:touches withEvent:event];
    } 
    
    @end
    
        5
  •  1
  •   Kendall Helmstetter Gelner    15 年前

        6
  •  0
  •   Jab    15 年前

    你也可以用两个手指轻击。当我回家的时候,我可以用一些代码来详细说明,但这就是我所使用的

    推荐文章