gpt4 book ai didi

ios - 使用 pangesture 在 UIImageView 图像上绘制贝塞尔路径

转载 作者:行者123 更新时间:2023-12-01 16:45:02 26 4
gpt4 key购买 nike

我有一个:

  • CustomView它是 UIView 的子类.
  • CustomImageView它是 UIImageView 的子类
  • CustomImageViewCustomView 的 subview
  • UIPanGestureRecognizer应用于另一个 UIImageView调用panImage
  • panImageCustomImageView 的 subview

  • 现在,我需要一边移动一边画画 panImageCustomImageView但它不应该在 CustomView 中绘制.

    以下是我编写的代码,但不起作用。
    需要帮忙。

    自定义 ImageView
    #import <UIKit/UIKit.h>
    #import "CustomImageView.h"
    @interface CustomView : UIView
    @property (nonatomic,strong) UIPanGestureRecognizer *panGesture;
    @property (nonatomic,strong) UIImageView *panImage;
    @property (nonatomic,strong) CustomImageView *backgroundImage;
    @end

    #import "CustomView.h"
    @implementation CustomView

    -(void) awakeFromNib {
    // inserting a background image
    self.backgroundImage = [[CustomImageView alloc] initWithImage:[UIImage imageNamed:@"ef-06-chartkey-yellow.jpg"]];
    CGRect imageFrame = CGRectMake(20,14, 450,450);
    self.backgroundImage.userInteractionEnabled = YES;
    self.backgroundImage.exclusiveTouch = YES;
    self.backgroundImage.frame = imageFrame;
    [self addSubview:self.backgroundImage];

    if (nil == self.panGesture) {
    self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self.backgroundImage
    action:@selector(handlePanGesture:)];
    }

    self.panImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"slider_thumb.png"]];
    CGRect panFramePoint = CGRectMake(10,10, self.panImage.frame.size.width, self.panImage.frame.size.height);
    self.panImage.frame = panFramePoint;
    self.panImage.userInteractionEnabled = YES;
    [self.panImage addGestureRecognizer:self.panGesture];
    [self.backgroundImage addSubview:self.panImage];
    }
    @end

    自定义 ImageView
    #import <UIKit/UIKit.h>
    @interface CustomImageView : UIImageView
    @property (nonatomic,strong) UIBezierPath *path;
    @end

    #import "CustomImageView.h"
    @implementation CustomImageView

    -(id) initWithImage:(UIImage *)image {
    self = [super initWithImage:image];
    if (self) {
    self.image = image;
    self.path = [[UIBezierPath alloc] init];
    self.path.lineWidth = 25;
    }
    return self;
    }

    - (void)drawRect:(CGRect)rect {
    // Drawing code
    [[UIColor redColor] setFill];
    [self.path stroke];
    }

    - (void) handlePanGesture:(UIPanGestureRecognizer *)recognizer {
    CGPoint translation = [recognizer translationInView:self];
    recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
    recognizer.view.center.y + translation.y);

    //NSLog(@"[recognizer locationInView:self] %f, %f",[recognizer locationInView:self.customLetterImage].x, [recognizer locationInView:self.customLetterImage].y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self];
    [self.path addLineToPoint:[recognizer locationInView:self]];
    [self setNeedsDisplayInRect:self.frame];
    }

    -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"CustomImageView touchesBegan");
    UITouch *mytouch = [touches anyObject];
    [self.path moveToPoint: [mytouch locationInView:self]];
    [self setNeedsDisplay];
    }

    -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"CustomImageView touchesMoved");
    }

    -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"CustomImageView touchesEnded");
    }

    -(void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"CustomImageView touchesCancelled");
    }

    @end

    最佳答案

    我通过在 customImageView 上方创建自定义 View 解决了这个问题。这样,我使用 pangesture 在 customImageView 上方的 View 上绘制。

    关于ios - 使用 pangesture 在 UIImageView 图像上绘制贝塞尔路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20727433/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com