iOS UIGestureRecognizer與UIMenuController(內容根據iOS編程)

来源:http://www.cnblogs.com/wang-com/archive/2016/10/11/5947224.html
-Advertisement-
Play Games

UIGestureRecognizer 對象會截取本應由視圖處理的觸摸事件。當某個UIGestureRecognizer對象識別出特定的手勢後,就會向指定的對象發送指定的消息。iOS SDK預設提供若幹中UIGestureRecoginezer對象。本章我們將繼續更新 JXTouchTracker ...


  UIGestureRecognizer 對象會截取本應由視圖處理的觸摸事件。當某個UIGestureRecognizer對象識別出特定的手勢後,就會向指定的對象發送指定的消息。iOS SDK預設提供若幹中UIGestureRecoginezer對象。本章我們將繼續更新 JXTouchTracker ,藉助由iOS SDK提供的三種 UIGestureRecogniezer對象,用戶可以選擇、移動、刪除線條。

  • UIGestureRecognizer子類

  在為應用添加手勢識別功能時,需要針對特定的手勢創建響應的UIGestureRecognizer子類對象,而不是直接使用UIGestureRecognizer對象。iOS SDK提供了多種能夠處理不同手勢的UIGestureRecognizer子類。

  使用UIGestureRecognizer子類對象時,除了要設置目標動作對,還要將該子類對象“附著”在某個視圖上。當該子類對象根據當前附著的視圖所發生的觸摸事件識別出相應的手勢時,就會向指定的目標對象發送指定的動作消息。由UIGestureRecognizer對象發出的動作消息都會遵守以下規範:

- (void)action:(UIGestureRecognizer *)gestureRecognizer

  UIGestureRecognizer對象在識別手勢時,會截取本應由其附著的視圖自行處理的觸摸事件。因此,附著了 UIGestureRecognizer 對象的視圖可能不會受到常規的 UIResponder 消息,例如,不會收到: - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 消息。

  • 用UITapGestureRecognizer對象識別點擊手勢

  下麵為我們應用添加一個功能,當用戶雙擊屏幕時,會清除屏幕上的所有線條。

#import "JXDrawView.h"
#import "JXLine.h"

@interface JXDrawView ()
/** 保存當前正在繪製線條 */
@property (nonatomic,strong) JXLine * currentLine;
/** 保存已經繪製完成的線條 */
@property (nonatomic,strong) NSMutableArray * finishedLines;
/** 保存正在繪製的多條直線 */
@property (nonatomic,strong) NSMutableDictionary * linesInProgress;

@end

@implementation JXDrawView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.linesInProgress = [NSMutableDictionary dictionary];
        self.finishedLines = [NSMutableArray array];
        self.backgroundColor = [UIColor grayColor];
        
        // 支持多點觸摸
        self.multipleTouchEnabled = YES;
        
        // 添加點擊事件
        UITapGestureRecognizer * doubleTapRecoginzer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
        doubleTapRecoginzer.numberOfTapsRequired = 2;
        [self addGestureRecognizer:doubleTapRecoginzer];
    }
    return self;
}

// 添加手勢
- (void)doubleTap:(UIGestureRecognizer *)tap {
    [self.linesInProgress removeAllObjects];
    [self.finishedLines removeAllObjects];
    [self setNeedsDisplay];
}

- (void)strokeLine:(JXLine *)line {
    UIBezierPath * bp = [UIBezierPath bezierPath];
    bp.lineWidth = 3;
    bp.lineCapStyle = kCGLineCapRound;
    
    [bp moveToPoint:line.begin];
    [bp addLineToPoint:line.end];
    [bp stroke];
}

- (void)drawRect:(CGRect)rect {
    // 用黑色表示已經繪製完成的線條
    [[UIColor blackColor] set];
    for (JXLine * line in self.finishedLines) {
        [self strokeLine:line];
    }
    
    // 用紅色繪製正在畫的線條
    [[UIColor redColor] set];
    for (NSValue * key in self.linesInProgress) {
        [self strokeLine:self.linesInProgress[key]];
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch * t in touches) {
        CGPoint location = [t locationInView:self];
        
        JXLine * line = [[JXLine alloc] init];
        line.begin = location;
        line.end = location;
        
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        self.linesInProgress[key] = line;
    }
    
    [self setNeedsDisplay];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch * t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        JXLine * line = self.linesInProgress[key];
        line.end = [t locationInView:self];
    }
    
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch *t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        JXLine * line = self.linesInProgress[key];
        
        [self.finishedLines addObject:line];
        [self.linesInProgress removeObjectForKey:key];
    }
    
    
    [self setNeedsDisplay];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    for (UITouch *t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        [self.linesInProgress removeObjectForKey:key];
    }
    [self setNeedsDisplay];
}


@end

  構建並運行,同時我們可以檢測觸摸事件發生的順序:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

- (void)doubleTap:(UIGestureRecognizer *)tap 

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

  由於 UIGestureRecognizer 對象會通過截取觸摸事件來識別手勢,因此在UIGestureRecognizer 對象識別出手勢之前,UIView 會收到所有 UIResponder 消息。對於 UITapGestureRecognizer來說,在識別出點擊手勢之前,UIView 會收到  - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 消息:在識別出點擊手勢之後,UITapGestureRecognizer 會自行處理相關觸摸事件,由這些觸摸事件所引發的 UIResponder 消息將不會再發送給 UIView 。直到 UITapGestureRecognizer 檢測出點擊手勢已經結束,UIView 才會重新收到 UIResponder 消息( - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event )

  為了在識別出點擊手勢之前避免向 UIView 發送 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 消息。我們需要在代碼中做如下修改:

#import "JXDrawView.h"
#import "JXLine.h"

@interface JXDrawView ()
/** 保存當前正在繪製線條 */
@property (nonatomic,strong) JXLine * currentLine;
/** 保存已經繪製完成的線條 */
@property (nonatomic,strong) NSMutableArray * finishedLines;
/** 保存正在繪製的多條直線 */
@property (nonatomic,strong) NSMutableDictionary * linesInProgress;

@end

@implementation JXDrawView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.linesInProgress = [NSMutableDictionary dictionary];
        self.finishedLines = [NSMutableArray array];
        self.backgroundColor = [UIColor grayColor];
        
        // 支持多點觸摸
        self.multipleTouchEnabled = YES;
        
        // 添加點擊事件
        UITapGestureRecognizer * doubleTapRecoginzer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
        doubleTapRecoginzer.numberOfTapsRequired = 2;
        // 為了避免在識別出點擊手勢之前出發touches手勢
        doubleTapRecoginzer.delaysTouchesBegan  = YES;
        [self addGestureRecognizer:doubleTapRecoginzer];
    }
    return self;
}

// 添加手勢
- (void)doubleTap:(UIGestureRecognizer *)tap {
    [self.linesInProgress removeAllObjects];
    [self.finishedLines removeAllObjects];
    [self setNeedsDisplay];
}

- (void)strokeLine:(JXLine *)line {
    UIBezierPath * bp = [UIBezierPath bezierPath];
    bp.lineWidth = 3;
    bp.lineCapStyle = kCGLineCapRound;
    
    [bp moveToPoint:line.begin];
    [bp addLineToPoint:line.end];
    [bp stroke];
}

- (void)drawRect:(CGRect)rect {
    // 用黑色表示已經繪製完成的線條
    [[UIColor blackColor] set];
    for (JXLine * line in self.finishedLines) {
        [self strokeLine:line];
    }
    
    // 用紅色繪製正在畫的線條
    [[UIColor redColor] set];
    for (NSValue * key in self.linesInProgress) {
        [self strokeLine:self.linesInProgress[key]];
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch * t in touches) {
        CGPoint location = [t locationInView:self];
        
        JXLine * line = [[JXLine alloc] init];
        line.begin = location;
        line.end = location;
        
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        self.linesInProgress[key] = line;
    }
    
    [self setNeedsDisplay];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch * t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        JXLine * line = self.linesInProgress[key];
        line.end = [t locationInView:self];
    }
    
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch *t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        JXLine * line = self.linesInProgress[key];
        
        [self.finishedLines addObject:line];
        [self.linesInProgress removeObjectForKey:key];
    }
    
    
    [self setNeedsDisplay];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    for (UITouch *t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        [self.linesInProgress removeObjectForKey:key];
    }
    [self setNeedsDisplay];
}


@end
  • 同時添加多種觸摸手勢

  接下來我們為應用中添加單擊手勢,讓用戶可以選擇屏幕上的線條

#import "JXDrawView.h"
#import "JXLine.h"

@interface JXDrawView ()
/** 保存當前正在繪製線條 */
@property (nonatomic,strong) JXLine * currentLine;
/** 保存已經繪製完成的線條 */
@property (nonatomic,strong) NSMutableArray * finishedLines;
/** 保存正在繪製的多條直線 */
@property (nonatomic,strong) NSMutableDictionary * linesInProgress;

@end

@implementation JXDrawView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.linesInProgress = [NSMutableDictionary dictionary];
        self.finishedLines = [NSMutableArray array];
        self.backgroundColor = [UIColor grayColor];
        
        // 支持多點觸摸
        self.multipleTouchEnabled = YES;
        
        // 添加點擊事件
        UITapGestureRecognizer * doubleTapRecoginzer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
        doubleTapRecoginzer.numberOfTapsRequired = 2;
        // 為了避免在識別出點擊手勢之前出發touches手勢
        doubleTapRecoginzer.delaysTouchesBegan  = YES;
        [self addGestureRecognizer:doubleTapRecoginzer];
        
        // 添加單機事件
        UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        tapRecognizer.delaysTouchesBegan = YES;
        [self addGestureRecognizer:tapRecognizer];
        
    }
    return self;
}
// 添加單機事件
- (void)tap:(UIGestureRecognizer *)tap {
    NSLog(@"%s",__func__);
}
// 添加手勢
- (void)doubleTap:(UIGestureRecognizer *)tap {
    [self.linesInProgress removeAllObjects];
    [self.finishedLines removeAllObjects];
    [self setNeedsDisplay];
}

- (void)strokeLine:(JXLine *)line {
    UIBezierPath * bp = [UIBezierPath bezierPath];
    bp.lineWidth = 3;
    bp.lineCapStyle = kCGLineCapRound;
    
    [bp moveToPoint:line.begin];
    [bp addLineToPoint:line.end];
    [bp stroke];
}

- (void)drawRect:(CGRect)rect {
    // 用黑色表示已經繪製完成的線條
    [[UIColor blackColor] set];
    for (JXLine * line in self.finishedLines) {
        [self strokeLine:line];
    }
    
    // 用紅色繪製正在畫的線條
    [[UIColor redColor] set];
    for (NSValue * key in self.linesInProgress) {
        [self strokeLine:self.linesInProgress[key]];
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch * t in touches) {
        CGPoint location = [t locationInView:self];
        
        JXLine * line = [[JXLine alloc] init];
        line.begin = location;
        line.end = location;
        
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        self.linesInProgress[key] = line;
    }
    
    [self setNeedsDisplay];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch * t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        JXLine * line = self.linesInProgress[key];
        line.end = [t locationInView:self];
    }
    
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch *t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        JXLine * line = self.linesInProgress[key];
        
        [self.finishedLines addObject:line];
        [self.linesInProgress removeObjectForKey:key];
    }
    
    
    [self setNeedsDisplay];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    for (UITouch *t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        [self.linesInProgress removeObjectForKey:key];
    }
    [self setNeedsDisplay];
}


@end

  構建並運行。可以發現,點擊一次可以正確識別出單擊手勢,控制台會輸出單擊方法信息;但是如果我們雙擊,應用將無法識別出正確的單擊手勢,單擊雙擊手勢方法都會執行。

  如果需要為視圖添加多種手勢,就需要考慮這些手勢之間的關係。雙擊手勢包含兩次單擊,為了避免 UITapGestureRecognizer 將雙擊時間分拆為兩個單擊事件,可以設置UITapGestureRecognizer 在單擊後暫時不進行識別,知道確定不是雙擊手勢後再識別為單擊手勢。

#import "JXDrawView.h"
#import "JXLine.h"

@interface JXDrawView ()
/** 保存當前正在繪製線條 */
@property (nonatomic,strong) JXLine * currentLine;
/** 保存已經繪製完成的線條 */
@property (nonatomic,strong) NSMutableArray * finishedLines;
/** 保存正在繪製的多條直線 */
@property (nonatomic,strong) NSMutableDictionary * linesInProgress;

@end

@implementation JXDrawView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.linesInProgress = [NSMutableDictionary dictionary];
        self.finishedLines = [NSMutableArray array];
        self.backgroundColor = [UIColor grayColor];
        
        // 支持多點觸摸
        self.multipleTouchEnabled = YES;
        
        // 添加點擊事件
        UITapGestureRecognizer * doubleTapRecoginzer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
        doubleTapRecoginzer.numberOfTapsRequired = 2;
        // 為了避免在識別出點擊手勢之前出發touches手勢
        doubleTapRecoginzer.delaysTouchesBegan  = YES;
        [self addGestureRecognizer:doubleTapRecoginzer];
        
        // 添加單機事件
        UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        tapRecognizer.delaysTouchesBegan = YES;
        // 用來防止將雙擊事件拆分為單擊
        [tapRecognizer requireGestureRecognizerToFail:doubleTapRecoginzer];
        [self addGestureRecognizer:tapRecognizer];
        
    }
    return self;
}
// 添加單機事件
- (void)tap:(UIGestureRecognizer *)tap {
    NSLog(@"%s",__func__);
}
// 添加手勢
- (void)doubleTap:(UIGestureRecognizer *)tap {
    [self.linesInProgress removeAllObjects];
    [self.finishedLines removeAllObjects];
    [self setNeedsDisplay];
}

- (void)strokeLine:(JXLine *)line {
    UIBezierPath * bp = [UIBezierPath bezierPath];
    bp.lineWidth = 3;
    bp.lineCapStyle = kCGLineCapRound;
    
    [bp moveToPoint:line.begin];
    [bp addLineToPoint:line.end];
    [bp stroke];
}

- (void)drawRect:(CGRect)rect {
    // 用黑色表示已經繪製完成的線條
    [[UIColor blackColor] set];
    for (JXLine * line in self.finishedLines) {
        [self strokeLine:line];
    }
    
    // 用紅色繪製正在畫的線條
    [[UIColor redColor] set];
    for (NSValue * key in self.linesInProgress) {
        [self strokeLine:self.linesInProgress[key]];
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch * t in touches) {
        CGPoint location = [t locationInView:self];
        
        JXLine * line = [[JXLine alloc] init];
        line.begin = location;
        line.end = location;
        
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        self.linesInProgress[key] = line;
    }
    
    [self setNeedsDisplay];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch * t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        JXLine * line = self.linesInProgress[key];
        line.end = [t locationInView:self];
    }
    
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch *t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        JXLine * line = self.linesInProgress[key];
        
        [self.finishedLines addObject:line];
        [self.linesInProgress removeObjectForKey:key];
    }
    
    
    [self setNeedsDisplay];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    for (UITouch *t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        [self.linesInProgress removeObjectForKey:key];
    }
    [self setNeedsDisplay];
}


@end

  構建並運行應用,單擊屏幕,UITapGestureRecognizer 會稍作停頓,確定是單擊手勢之後再執行 tap: 方法。而雙擊之後就不會執行這個方法了。

  現在為項目 添加單擊選擇線條功能。

#import "JXDrawView.h"
#import "JXLine.h"

@interface JXDrawView ()

/** 保存已經繪製完成的線條 */
@property (nonatomic,strong) NSMutableArray * finishedLines;
/** 保存正在繪製的多條直線 */
@property (nonatomic,strong) NSMutableDictionary * linesInProgress;

/** 保存選中的線條 */
@property (nonatomic,weak) JXLine * selectedLine;

@end

@implementation JXDrawView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.linesInProgress = [NSMutableDictionary dictionary];
        self.finishedLines = [NSMutableArray array];
        self.backgroundColor = [UIColor grayColor];
        
        // 支持多點觸摸
        self.multipleTouchEnabled = YES;
        
        // 添加點擊事件
        UITapGestureRecognizer * doubleTapRecoginzer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
        doubleTapRecoginzer.numberOfTapsRequired = 2;
        // 為了避免在識別出點擊手勢之前出發touches手勢
        doubleTapRecoginzer.delaysTouchesBegan  = YES;
        [self addGestureRecognizer:doubleTapRecoginzer];
        
        // 添加單機事件
        UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        tapRecognizer.delaysTouchesBegan = YES;
        // 用來防止將雙擊事件拆分為單擊
        [tapRecognizer requireGestureRecognizerToFail:doubleTapRecoginzer];
        [self addGestureRecognizer:tapRecognizer];
        
    }
    return self;
}
// 添加單機事件
- (void)tap:(UIGestureRecognizer *)tap {
    NSLog(@"%s",__func__);
    
    CGPoint point = [tap locationInView:self];
    self.selectedLine = [self lineAtPoint:point];
    
    [self setNeedsDisplay];
}
// 添加手勢
- (void)doubleTap:(UIGestureRecognizer *)tap {
    [self.linesInProgress removeAllObjects];
    [self.finishedLines removeAllObjects];
    [self setNeedsDisplay];
}


// 畫線
- (void)strokeLine:(JXLine *)line {
    UIBezierPath * bp = [UIBezierPath bezierPath];
    bp.lineWidth = 3;
    bp.lineCapStyle = kCGLineCapRound;
    
    [bp moveToPoint:line.begin];
    [bp addLineToPoint:line.end];
    [bp stroke];
}

- (void)drawRect:(CGRect)rect {
    // 用黑色表示已經繪製完成的線條
    [[UIColor blackColor] set];
    for (JXLine * line in self.finishedLines) {
        [self strokeLine:line];
    }
    
    // 用紅色繪製正在畫的線條
    [[UIColor redColor] set];
    for (NSValue * key in self.linesInProgress) {
        [self strokeLine:self.linesInProgress[key]];
    }
    
    if (self.selectedLine) {
        [[UIColor greenColor] set];
        [self strokeLine:self.selectedLine];
    }
    
}

// 根據傳入的位置找出距離最近的那個對象
- (JXLine *)lineAtPoint:(CGPoint)p {
    
    // 找出離p最近的JXLine對象
    for (JXLine * line in self.finishedLines) {
        CGPoint start = line.begin;
        CGPoint end = line.end;
        
        // 檢查線條的若幹個點
        for (float t = 0.0; t <= 1.0; t += 0.05) {
            float x = start.x + t * (end.x - start.x);
            float y = start.y + t * (end.y - start.y);
            
            // 如果線條的某個點和p的距離在20點以內,就返迴響應的JXLIne對象
            if (hypot(x-p.x, y-p.y) < 20.0) {
                return line;
            }
        }
    }
    
    // 如果沒有找到符合條件的線條,就返回nil
    return nil;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch * t in touches) {
        CGPoint location = [t locationInView:self];
        
        JXLine * line = [[JXLine alloc] init];
        line.begin = location;
        line.end = location;
        
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        self.linesInProgress[key] = line;
    }
    
    [self setNeedsDisplay];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch * t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        JXLine * line = self.linesInProgress[key];
        line.end = [t locationInView:self];
    }
    
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch *t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        JXLine * line = self.linesInProgress[key];
        
        [self.finishedLines addObject:line];
        [self.linesInProgress removeObjectForKey:key];
    }
    
    
    [self setNeedsDisplay];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    for (UITouch *t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        [self.linesInProgress removeObjectForKey:key];
    }
    [self setNeedsDisplay];
}


@end

  構建並運行:

  • UIMenuController

  下麵我們要為應用添加另一個功能:當用戶選中某根線條時,我們要在用戶手指按下的位置顯示一個菜單。這個菜單要為用戶提供一個刪除選項。iOS提供了一個名為 UIMenuController 的類,可以用來顯示這類菜單。

  每個iOS應用只有一個 UIMenuController 對象。當應用要顯示該對象時,要現為他設置一組 UIMenuItem 對象,然後設置顯示位置,最後將其設置為可見。

#import "JXDrawView.h"
#import "JXLine.h"

@interface JXDrawView ()

/** 保存已經繪製完成的線條 */
@property (nonatomic,strong) NSMutableArray * finishedLines;
/** 保存正在繪製的多條直線 */
@property (nonatomic,strong) NSMutableDictionary * linesInProgress;

/** 保存選中的線條 */
@property (nonatomic,weak) JXLine * selectedLine;

@end

@implementation JXDrawView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        self.linesInProgress = [NSMutableDictionary dictionary];
        self.finishedLines = [NSMutableArray array];
        self.backgroundColor = [UIColor grayColor];
        
        // 支持多點觸摸
        self.multipleTouchEnabled = YES;
        
        // 添加點擊事件
        UITapGestureRecognizer * doubleTapRecoginzer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
        doubleTapRecoginzer.numberOfTapsRequired = 2;
        // 為了避免在識別出點擊手勢之前出發touches手勢
        doubleTapRecoginzer.delaysTouchesBegan  = YES;
        [self addGestureRecognizer:doubleTapRecoginzer];
        
        // 添加單機事件
        UITapGestureRecognizer * tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        tapRecognizer.delaysTouchesBegan = YES;
        // 用來防止將雙擊事件拆分為單擊
        [tapRecognizer requireGestureRecognizerToFail:doubleTapRecoginzer];
        [self addGestureRecognizer:tapRecognizer];
        
    }
    return self;
}
// 添加單機事件
- (void)tap:(UIGestureRecognizer *)tap {

    CGPoint point = [tap locationInView:self];
    self.selectedLine = [self lineAtPoint:point];
    
    // 當有選中線條時
    if (self.selectedLine) {
        
        // 是視圖成為 UIMenuItem 動作消息的目標
        [self becomeFirstResponder];
        
        // 獲取 UIMenuController 對象
        UIMenuController * menu = [UIMenuController sharedMenuController];
        
        // 創建一個新的標題為“Delete”的UIMenuItem對象
        UIMenuItem * deleteItem = [[UIMenuItem alloc] initWithTitle:@"Delete" action:@selector(deleteLine:)];
        menu.menuItems = @[deleteItem];
        
        // 先為 UIMenuController 對象設置顯示區域,然後將其設置為可見
        [menu setTargetRect:CGRectMake(point.x, point.y, 2, 2) inView:self];
        [menu setMenuVisible:YES animated:YES];
    } else {
        // 如果沒有選中的線條,就隱藏 UIMenuController 對象
        [[UIMenuController sharedMenuController] setMenuVisible:NO animated:YES];
    }
    [self setNeedsDisplay];
}
// 添加手勢
- (void)doubleTap:(UIGestureRecognizer *)tap {
    [self.linesInProgress removeAllObjects];
    [self.finishedLines removeAllObjects];
    [self setNeedsDisplay];
}


// 畫線
- (void)strokeLine:(JXLine *)line {
    UIBezierPath * bp = [UIBezierPath bezierPath];
    bp.lineWidth = 3;
    bp.lineCapStyle = kCGLineCapRound;
    
    [bp moveToPoint:line.begin];
    [bp addLineToPoint:line.end];
    [bp stroke];
}

- (void)drawRect:(CGRect)rect {
    // 用黑色表示已經繪製完成的線條
    [[UIColor blackColor] set];
    for (JXLine * line in self.finishedLines) {
        [self strokeLine:line];
    }
    
    // 用紅色繪製正在畫的線條
    [[UIColor redColor] set];
    for (NSValue * key in self.linesInProgress) {
        [self strokeLine:self.linesInProgress[key]];
    }
    
    if (self.selectedLine) {
        [[UIColor greenColor] set];
        [self strokeLine:self.selectedLine];
    }
    
}

// 根據傳入的位置找出距離最近的那個對象
- (JXLine *)lineAtPoint:(CGPoint)p {
    
    // 找出離p最近的JXLine對象
    for (JXLine * line in self.finishedLines) {
        CGPoint start = line.begin;
        CGPoint end = line.end;
        
        // 檢查線條的若幹個點
        for (float t = 0.0; t <= 1.0; t += 0.05) {
            float x = start.x + t * (end.x - start.x);
            float y = start.y + t * (end.y - start.y);
            
            // 如果線條的某個點和p的距離在20點以內,就返迴響應的JXLIne對象
            if (hypot(x-p.x, y-p.y) < 20.0) {
                return line;
            }
        }
    }
    
    // 如果沒有找到符合條件的線條,就返回nil
    return nil;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch * t in touches) {
        CGPoint location = [t locationInView:self];
        
        JXLine * line = [[JXLine alloc] init];
        line.begin = location;
        line.end = location;
        
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        self.linesInProgress[key] = line;
    }
    
    [self setNeedsDisplay];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch * t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        JXLine * line = self.linesInProgress[key];
        line.end = [t locationInView:self];
    }
    
    [self setNeedsDisplay];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    for (UITouch *t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        JXLine * line = self.linesInProgress[key];
        
        [self.finishedLines addObject:line];
        [self.linesInProgress removeObjectForKey:key];
    }
    
    
    [self setNeedsDisplay];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    for (UITouch *t in touches) {
        NSValue * key = [NSValue valueWithNonretainedObject:t];
        [self.linesInProgress removeObjectForKey:key];
    }
    [self setNeedsDisplay];
}


@end

  要顯示 UIMenuController 對象,還要滿足一個條件:顯示UIMenuController對象的UIView對象必須是當前UIWindow對象的第一響應對象。這也是為什麼在 tap: 方法中起始部分會向JXDrawView 發送  becomeFirstResponder 消息。如果要將某個自定義的UIView子類對象設置為第一響應對象,就必須覆蓋該對象的  canBecomeFirstResponder 方法:

// 將某個自定義的UIView子類對象設置為第一響應對象,就必須覆蓋此類方法
- (BOOL)canBecomeFirstResponder {
    return YES;
}

  實現刪除選中線條方法:

#import "JXDrawView.h"
#import "
              
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 基於jquery-1.10.1的添加刪除員工的一個簡單的HTML界面 ...
  • 我本人就是一個用戶,當我在使用全國軟考網是所遭遇的問題讓我頭痛,報名和查看信息要登陸賬戶時,瀏覽器總是報錯,彈窗提示說必須使用IE6版本JS才能正常運行,這樣真的很讓我很無語。還有在一些個別的企業網站,打開頁面通常需要5S的時間,期間頁面呈現空白,也會聽到散熱風扇“呼呼”地吹著熱風,我查看了一下源代 ...
  • (-1)寫在前面 我用的是chrome49,這篇是為後續做準備。重要性的調整以及畢業資料的整體導致最近沒看JQuery和H5特效,以後只能晚上看了。 (0)準備 div長寬都為300px,我們一張大小小於div的圖片1.jpg為例,將1.jpg設置為背景圖片,添加樣式background-repea ...
  • 使用CSS3實現的一個高亮光弧效果,當滑鼠hover到某一個元素上時,一道光弧從左向右閃過,效果如下: 代碼如下: 存下來備用! ...
  • 產品姐姐想法多,點擊input項才能聚焦進行操作,點擊外部不能有反應 好了。。。直入正題 為了讓標簽更加語義化,在表單項中,我們往往會使用label進行包裹 在移動平臺頁面的開發中,為了讓表單項的可點區域變大而更好的操作,label可提供相應的便利。 但有時,我們只是需要label標簽,卻不希望可點 ...
  • 通過設置table width=“100%”table-layout="fixed" 解決 ...
  • 記得剛開始學習js的時候寫過一次掃雷,一個下午的時間被計算搞死,整個頭是暈乎。 入職後,蹭著空閑的時間隨手寫了一個掃雷。 直接上代碼了 初始化時生成元素並且給元素綁定屬性(打開、地雷、地雷數、旗幟),PS:data()方法是真的好用 動態生成元素並且通過data給元素綁定初始屬性 最開始的時候獲取九 ...
  • 在項目中,用到一個功能,就是不同文字和不同顏色字體選擇, 界面如下:,效果如下: 這個功能主要用到了textview的幾個代理辦法,其中一個重要的問題就是,在英文下和英文下的不同判斷方式,以及是否有追加字: 字體,顏色,字元用語封裝起來,便於後續操作, 在項目中,每個字元用一個對象保存在數組中。 我 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...