How can I draw an arrow using Core Graphics?

后端 未结 4 1285
梦毁少年i
梦毁少年i 2020-12-02 04:58

I need to draw line with arrow on its end in my Draw app. I\'m not good in trigonometry, so can\'t solve this problem.

The user put his finger on the screen and d

4条回答
  •  佛祖请我去吃肉
    2020-12-02 05:52

    //This is the integration into the view of the previous exemple
    //Attach the following class to your view in the xib file
    
    #import 
    
    @interface Arrow : UIView
    
    @end
    
    #import "Arrow.h"
    #import "UIBezierPath+dqd_arrowhead.h"
    
    @implementation Arrow
    {
        CGPoint startPoint;
        CGPoint endPoint;
        CGFloat tailWidth;
        CGFloat headWidth;
        CGFloat headLength;
        UIBezierPath *path;
    
    }
    
    
    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        if (self = [super initWithCoder:aDecoder])
        {
            [self setMultipleTouchEnabled:NO];
            [self setBackgroundColor:[UIColor whiteColor]];
    
        }
        return self;
    }
    
    - (void)drawRect:(CGRect)rect {
    
        [[UIColor redColor] setStroke];
        tailWidth = 4;
        headWidth = 8;
        headLength = 8;
        path = [UIBezierPath dqd_bezierPathWithArrowFromPoint:(CGPoint)startPoint
                                                      toPoint:(CGPoint)endPoint
                                                    tailWidth:(CGFloat)tailWidth
                                                    headWidth:(CGFloat)headWidth
                                                   headLength:(CGFloat)headLength];
        [path setLineWidth:2.0];
    
        [path stroke];
    
    }
    - (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
    {
        UITouch* touchPoint = [touches anyObject];
        startPoint = [touchPoint locationInView:self];
        endPoint = [touchPoint locationInView:self];
    
    
        [self setNeedsDisplay];
    }
    
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch* touch = [touches anyObject];
        endPoint=[touch locationInView:self];
        [self setNeedsDisplay];
    }
    
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch* touch = [touches anyObject];
        endPoint = [touch locationInView:self];
        [self setNeedsDisplay];
    }
    
    
    @end
    

提交回复
热议问题