ios - 帮助循环、存储和使用 CGPoints

标签 ios for-loop uibutton cgpoint

我有一些 UIButton 可以移动到不同的位置,这些位置由名为 totalSmallButtonLocations 的数组预先确定。所以我通过 availableLocations,一个 NSMutableArray,分配不同的 CGPoints,并从 availableLocations 中删除这些点。一旦完成,就会有一个 UIView 动画将按钮移动到新位置。

目前这是可行的,但正如您在下面看到的那样,它很长,(在实际项目中还有更多的按钮和位置):

int randomNumber;
NSValue *val;
CGPoint a, b, c;
NSMutableArray *availableLocations;
availableLocations = [NSMutableArray arrayWithArray:totalSmallButtonLocations];

randomNumber = arc4random_uniform(3);
val = [availableLocations objectAtIndex:randomNumber];
a = [val CGPointValue];
[availableLocations removeObjectAtIndex:randomNumber];
randomNumber = arc4random_uniform(13);
val = [availableLocations objectAtIndex:randomNumber];
b = [val CGPointValue];
[availableLocations removeObjectAtIndex:randomNumber];
randomNumber = arc4random_uniform(12);
val = [availableLocations objectAtIndex:randomNumber];
c = [val CGPointValue];

[UIView animateWithDuration:0.5 animations:^{
    button1.center = a;
    button2.center = b;
    button3.center = c;

所以我正在尝试创建一个循环,但 CGPoint 方面导致了一些问题。我认为我的循环没问题,但我不知道如何在动画部分分配它,它抛出了错误:

“从不兼容的类型 id 分配给‘CGPoint’(又名‘struct CGPoint’)”:

int randomNumber;
NSValue *val;
CGPoint a;
NSMutableArray *availableLocations;
availableLocations = [NSMutableArray arrayWithArray:totalSmallButtonLocations];
NSMutableArray *points = [NSMutableArray array];

for(int looper = 14; looper > 0; looper--)
{
    randomNumber = arc4random_uniform(looper);
    val = [availableLocations objectAtIndex:randomNumber];
    a = [val CGPointValue];
    [points addObject:[ NSValue valueWithCGPoint:a]];
}
[UIView animateWithDuration:0.5 animations:^{
    button1.center = [points objectAtIndex:1];
    button2.center = [points objectAtIndex:2];
    button3.center = [points objectAtIndex:3];

最佳答案

问题是您得到的是指向 CGPoint 的指针,而不是 CGPoint 本身。问题出在 [points objectAtIndex:1]; 您获取的是对象而不是结构 CGPoint。您只需像这样包装它 [[points objectAtIndex:1] CGPointValue]; 并且警告应该消失。

关于ios - 帮助循环、存储和使用 CGPoints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13828888/

相关文章:

ios - 为什么这个 UIButton 图像不会显示在自定义 UITableViewCell 中?

iphone - 一周后根据第一个字段中输入的日期自动显示第二个字段中的日期

ios - 快速具有多个部分的 TableView

objective-c - Objective-C : How to add "plus" button in cell

iphone - 如果密码正确,如何切换 iPhone View

java - 如何将两个不同长度的 int 数组相加?

ios - 在 iOS 上是否有替代 opencore amr 将 wav 转换为 amr 的方法?

objective-c - 在 NSString 中显示时如何使 CGFloat 值更短?

java - for 循环不迭代我的 HashMap 键集

java - 用两个 for 循环简化计数(使用流)