• 通过path绘制点击区域


    通过path绘制点击区域

    效果

    源码

    https://github.com/YouXianMing/Animations

    //
    //  TapDrawImageView.h
    //  TapDrawImageView
    //
    //  Created by YouXianMing on 16/5/9.
    //  Copyright © 2016年 YouXianMing. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    #import "TapDrawPathManager.h"
    @class TapDrawImageView;
    
    static NSString *tapDrawImageViewNormalState    = @"normalState";
    static NSString *tapDrawImageViewHighlightState = @"highlightState";
    static NSString *tapDrawImageDisableState       = @"disableState";
    
    @protocol TapDrawImageViewDelegate <NSObject>
    
    - (void)tapDrawImageView:(TapDrawImageView *)tapImageView selectedPathManager:(TapDrawPathManager *)pathManager;
    
    @end
    
    @interface TapDrawImageView : UIView
    
    @property (nonatomic, weak)   id <TapDrawImageViewDelegate>  delegate;
    
    @property (nonatomic, strong) NSMutableArray  <TapDrawPathManager *>  *pathManagers;
    
    @end
    //
    //  TapDrawImageView.m
    //  TapDrawImageView
    //
    //  Created by YouXianMing on 16/5/9.
    //  Copyright © 2016年 YouXianMing. All rights reserved.
    //
    
    #import "TapDrawImageView.h"
    
    @interface TapDrawImageView ()
    
    @property (nonatomic, strong) TapDrawPathManager  *currentSelectedManager;
    @property (nonatomic, strong) UIImageView         *imageView;
    
    @end
    
    @implementation TapDrawImageView
    
    - (instancetype)initWithFrame:(CGRect)frame {
        
        if (self = [super initWithFrame:frame]) {
            
            self.pathManagers    = [NSMutableArray array];
            self.backgroundColor = [UIColor clearColor];
        }
        
        return self;
    }
    
    - (void)drawRect:(CGRect)rect {
        
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        [self.pathManagers enumerateObjectsUsingBlock:^(TapDrawPathManager *pathManager, NSUInteger idx, BOOL *stop) {
            
            TapDrawObject *drawObject = [pathManager.colorsType objectForKey:pathManager.currentDrawType];
            
            // Set Fill Color.
            {
                CGFloat red   = 0;
                CGFloat green = 0;
                CGFloat blue  = 0;
                CGFloat alpha = 0;
                [drawObject.fillColor getRed:&red green:&green blue:&blue alpha:&alpha];
                CGContextSetRGBFillColor(context, red, green, blue, alpha);
            }
            
            // Set Stroke Color.
            {
                CGFloat red   = 0;
                CGFloat green = 0;
                CGFloat blue  = 0;
                CGFloat alpha = 0;
                [drawObject.strokeColor getRed:&red green:&green blue:&blue alpha:&alpha];
                CGContextSetRGBStrokeColor(context, red, green, blue, alpha);
            }
            
            pathManager.path.lineWidth = drawObject.lineWidth;
            [pathManager.path fill];
            [pathManager.path stroke];
        }];
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        
        UITouch *touch     = [touches anyObject];
        CGPoint touchPoint = [touch locationInView:self];
        
        [self.pathManagers enumerateObjectsUsingBlock:^(TapDrawPathManager *pathManager, NSUInteger idx, BOOL *stop) {
            
            if ([pathManager.path containsPoint:touchPoint]) {
                
                if (self.delegate && [self.delegate respondsToSelector:@selector(tapDrawImageView:selectedPathManager:)]) {
                    
                    [self.delegate tapDrawImageView:self selectedPathManager:pathManager];
                }
                
                if ([pathManager.currentDrawType isEqualToString:tapDrawImageDisableState] == NO) {
                    
                    pathManager.currentDrawType = tapDrawImageViewHighlightState;
                    _currentSelectedManager     = pathManager;
                    [self setNeedsDisplay];
                }
                
                *stop = YES;
            }
        }];
    }
    
    - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
    
        if (_currentSelectedManager) {
            
            _currentSelectedManager.currentDrawType = tapDrawImageViewNormalState;
            [self setNeedsDisplay];
        }
        _currentSelectedManager = nil;
    }
    
    - (void)touchesCancelled:(nullable NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event {
    
        if (_currentSelectedManager) {
            
            _currentSelectedManager.currentDrawType = tapDrawImageViewNormalState;
            [self setNeedsDisplay];
        }
        _currentSelectedManager = nil;
    }
    
    @end
  • 相关阅读:
    Jenkins持续集成邮件发送
    基于appium快速实现H5自动化测试
    Linux常用命令学习一
    BZOJ4372烁烁的游戏——动态点分治+线段树(点分树套线段树)
    BZOJ3730震波——动态点分治+线段树(点分树套线段树)
    BZOJ1014[JSOI2008]火星人——非旋转treap+二分答案+hash
    BZOJ1299[LLH邀请赛]巧克力棒——Nim游戏+搜索
    BZOJ1115[POI2009]石子游戏——阶梯Nim游戏
    BZOJ3110[Zjoi2013]K大数查询——权值线段树套线段树
    BZOJ5343[Ctsc2018]混合果汁——主席树+二分答案
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/5618626.html
Copyright © 2020-2023  润新知