在这里,问题不是什么,但我对objectiveC
了解不多。所以问题是我正在处理一个项目,用户点击图像并使用 UITapGestureRecognizer
我必须将点击的位置存储在我的 array
中。我不知道每次用户点击 view
时,CGPoint
值都存储在 NSMutableArray
中是怎么可能的。
dataArray = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < [getResults count]; i++) {
[dataArray addObject:tappedPoint];
NSLog(@"RESULT TEST %@", dataArray);
}
最佳答案
您需要继承 UIView
并实现方法 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
以获得触摸回调。其次,不能将触摸位置 (CGPoint
) 添加到 NSMutableArray
,但它的包装类可以 (NSValue
)。这将是您要实现的目标的非常基本的实现。
// in UIView subclass
// Precondition: you have an NSMutableArray named `someMutableArray'
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint touchLocation = [[touches anyObject] locationInView:self];
NSValue *wrapper = [NSValue valueWithCGPoint:touchLocation];
[someMutableArray addObject:wrapper];
}
如果您以后想遍历这些触摸位置,您所要做的就是快速枚举数组并解包 NSValue
。
for (NSValue *wrapper in someMutableArray) {
CGPoint touchLocation = [wrapper CGPointValue];
}
关于ios - 如何使用 for 循环在 NSMutableArray 中添加对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25092121/