gpt4 book ai didi

iphone - 在使用 CGContext 绘制的 UIImage 中获取触摸

转载 作者:行者123 更新时间:2023-12-01 19:22:38 25 4
gpt4 key购买 nike

我正在尝试检测用户是否点击了带有 CGContext 和如下路径的绘图中的图像(这发生在名为 SwbPlate 的 customview 类中:

  SwbPlateImage * image;
CGFloat startAngle;
CGFloat endAngle;

CGContextRef context = UIGraphicsGetCurrentContext();


for (image in plateImages) {
endAngle = [EntGeometry pointToRadian:[image endPoint] withCenterPoint:CGPointMake(self.bounds.size.width/2 , self.bounds.size.height /2)];
startAngle = [EntGeometry pointToRadian:[image startPoint] withCenterPoint:CGPointMake(self.bounds.size.width/2 , self.bounds.size.height /2)];
imageToDraw = [image plateImg];
NSLog(@"%@", image);
CGContextSaveGState(context);
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, self.bounds.size.width/2 , self.bounds.size.height/2);
CGPathAddArc( path,
NULL,
self.bounds.size.width/2,
self.bounds.size.height/2,
(self.bounds.size.width/2),
endAngle,
startAngle,
0);
CGPathCloseSubpath(path);

CGContextAddPath(context, path);
CGContextClip(context);

CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(context, rect, [imageToDraw CGImage]);
CGContextRestoreGState(context);

//RVE MOD
image.imageContext = context;

然后我尝试查看用户是否在此上下文中移动了某个点,但这似乎不起作用
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];



NSArray *allTouches = [touches allObjects];
UITouch *touch = [allTouches objectAtIndex:0];
SwbDragProduct*dragProd;





for (UIView *view in self.view.subviews) {

if ([view isKindOfClass:[SwbPlateView class]] &(CGRectContainsPoint([view frame], [touch locationInView:self.view])) ) {
NSLog(@" We touched in the PLATE");
for (SwbPlateImage *imgView in plateView.plateImages){

if(CGContextPathContainsPoint(imgView.imageContext, [touch locationInView:self.view],kCGPathFillStroke)){
NSLog(@" We Touched within the IMAGE");
}
}
}

最佳答案

在我看来,您有 SwbPlateImage 局部变量,您在其中分配上下文,当您检查上下文时,该变量在 for 循环中不可用。您将需要管理和存储 SwbPlateImage 的上下文对象的 Singleton 类(例如 SwbPlateImageSingleTon)。如果您可以提供有关问题中提到的类(class)的更多详细信息,我可以帮助您编写单例类(class)。

从您的问题来看,您需要将 CGContextRef 的 NSMutableArray(say contextArray) 作为单例类中的强对象。与其将上下文分配给 image.imageContext,不如将上下文添加到此数组中。

 [[SwbPlateImageSingleTon sharedInstance] contextArray] addObject:context]

然后你的内部循环将如下所示。
   for (CGContextRef context in [[SwbPlateImageSingleTon sharedInstance] contextArray]) {
if(CGContextPathContainsPoint(context, [touch locationInView:self.view],kCGPathFillStroke)) {
NSLog(@" We Touched within the IMAGE");
}
}

单例类

SwbPlateImageSingleTon.h
@interface SwbPlateImageSingleTon : NSObject
@property (strong, nonatomic) NSMutableArray *contextArray;
+ (SwbPlateImageSingleTon*)sharedInstance;
@end

SwbPlateImageSingleTon.m
#import "SwbPlateImageSingleTon.h"

@implementation SwbPlateImageSingleTon
static SwbPlateImageSingleTon *sharedObject = nil;

+ (SwbPlateImageSingleTon*)sharedInstance
{
@synchronized(self) {
if (!sharedObject) {
sharedObject = [[SwbPlateImageSingleTon alloc] init];
}
return sharedObject;
}
}
@end

关于iphone - 在使用 CGContext 绘制的 UIImage 中获取触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9415896/

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