首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AnyObject到CGPoint

AnyObject到CGPoint
EN

Stack Overflow用户
提问于 2015-01-14 00:18:14
回答 1查看 1.2K关注 0票数 2

我似乎想不出这一点,我有一个返回NSArray的objects函数,我确信NSArray中的数据包含CGPoint对象,在这个世界中,我如何将它转换成一个数组

这是函数

代码语言:javascript
复制
+(NSArray *)translatePoints:(NSArray *)points fromView:(UIView *)fromView toView:(UIView *)toView
{
    NSMutableArray *translatedPoints = [NSMutableArray new];

    // The points are provided in a dictionary with keys X and Y
    for (NSDictionary *point in points) {
        // Let's turn them into CGPoints
        CGPoint pointValue = CGPointMake([point[@"X"] floatValue], [point[@"Y"] floatValue]);
        // Now translate from one view to the other
        CGPoint translatedPoint = [fromView convertPoint:pointValue toView:toView];
        // Box them up and add to the array
        [translatedPoints addObject:[NSValue valueWithCGPoint:translatedPoint]];
    }

    return [translatedPoints copy];
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-14 01:29:49

translatesPoints方法返回一个NSArray,其中包含包装CGPointNSValues。

代码语言:javascript
复制
let arr:NSArray = [NSValue(CGPoint: CGPointMake(1,2)), NSValue(CGPoint: CGPointMake(3,4))]

您可以从这个数组中获取值并对它们调用CGPointValue()

代码语言:javascript
复制
for val in arr as [NSValue] {
    let point = val.CGPointValue()
    println("CGPoint = (\(point.x), \(point.y))")
}

如果需要,可以将整个NSArray转换为CGPoint的Swift数组,如下所示:

代码语言:javascript
复制
let points = (arr as [NSValue]).map({$0.CGPointValue()})

现在,points有了[CGPoint]类型。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27933893

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档