gpt4 book ai didi

objective-c - 将 UITouch 存储在 NSArray 中

转载 作者:行者123 更新时间:2023-11-29 04:24:01 25 4
gpt4 key购买 nike

我正在尝试将 UITouches 存储在字典或数组中,但遇到了一些麻烦。存储 CGPoints 工作正常,但存储 UITouches 则不行。

进一步解释:当触摸开始时初始化数组,将每个 UITouch 存储在一个数组中,当触摸结束时我想输出该数组。我已经寻找了一段时间,但没有找到任何示例代码来执行此操作。

NSMutableArray *touchesArray;
NSMutableArray *pointArray;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

touchesArray = [[NSMutableArray alloc] init];

pointArray = [[NSMutableArray alloc] init];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self];
[pointArray addObject:[NSValue valueWithCGPoint: currentPoint]];

[touchesArray addObject:[NSValue valueWithPointer:(__bridge const void *)(touch)]];

for (UITouch *touch in touches)
{
NSLog(@"x location %f",[touch locationInView:self].x);
}


}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

for (UITouch *touch in touches)
{
NSLog(@"%f",[touch locationInView:self].x);
}

for(id p in pointArray){
NSValue *val = p;
CGPoint point = [val CGPointValue];
NSLog(@"%f",point.x);
}

//ERROR!
for(id p in touchesArray){
NSValue *val = p;
UITouch *t = (UITouch*) p;
NSLog(@"%@",[t timestamp]);
}

}

最佳答案

使用包装对象对我有用。 UITouch总是指向同一个地址。

#import "TouchInfo.h"

@implementation TouchInfo

@synthesize timestamp;
@synthesize location;

@end


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
int ns = [touches count];
NSLog(@"number of touches %i",ns);
for(UITouch *touch in touches)
{
NSNumber *val = [NSNumber numberWithFloat:[touch timestamp]];
TouchInfo *touchInfo = [[TouchInfo alloc] init];
touchInfo.location = [touch locationInView:self];
touchInfo.timestamp = [touch timestamp];
}
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

int nt = [touchesArray count];
for (int i = 0; i < nt; i++){
TouchInfo *touchInfo = (TouchInfo*)[touchesArray objectAtIndex:i];
NSLog(@"%f %f %f",touchInfo.timestamp,touchInfo.location.x,touchInfo.location.y);

}
}

关于objective-c - 将 UITouch 存储在 NSArray 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12618707/

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