ios - 如何使用 for 循环在 NSMutableArray 中添加对象?

标签 ios objective-c nsmutablearray uitapgesturerecognizer

在这里,问题不是什么,但我对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/

相关文章:

nsmutablearray - 从前一个 View Controller 中的数组中获取数据,将其放入另一个 TableViewController

objective-c - iOS 7 过度导航栏按钮填充

ios - Mapkit中如何增加userLocation标注的半径

objective-c - NSMutableArray 添加/删除对象 + NSUserDefaults

ios - UI界面方向问题

iphone - 核心图 : Allow horizontal scrolling in positive quadrant only

objective-c - 用 NSArray 替换 NSMutableArray 的内容

ios - 是否可以在 iOS 上编译和使用 xapian、clucene 或 lucy?

ios - fatal error : Index out of range - UITableViewController - How to handle/catch this error Swift

iphone - 在测试用户上安装应用