objective-c 将 CGPoint 转换为 NSValue

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/772958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:25:51  来源:igfitidea点击:

Converting a CGPoint to NSValue

objective-ciphonecgpointnsvalue

提问by CiNN

In CABasicAnimation.fromValueI want to convert a CGPointto a "class" so I used NSValuevalueWithPointbut in device mode or simulator one is not working... need to use NSMakePointor CGPointMakeif in device or simulator.

CABasicAnimation.fromValue我想将 a 转换CGPoint为“类”,所以我使用了NSValuevalueWithPoint但在设备模式或模拟器中一个不起作用......需要使用NSMakePoint或者CGPointMake如果在设备或模拟器中。

回答by ashcatch

There is a UIKitaddition to NSValuethat defines a function

还有一个定义函数的UIKit补充NSValue

+ (NSValue *)valueWithCGPoint:(CGPoint)point

See iPhone doc

请参阅iPhone 文档

回答by DanSkeel

@ashcatch 's answer is very helpful, but consider that those methods from addition copy values, when native NSValuemethods store pointers! Here is my code checking it:

@ashcatch 的回答非常有帮助,但考虑到那些来自加法复制值的NSValue方法,当本机方法存储指针时!这是我的代码检查它:

CGPoint point = CGPointMake(2, 4);
NSValue *val = [NSValue valueWithCGPoint:point];
point.x = 10;
CGPoint newPoint = [val CGPointValue];

here newPoint.x = 2; point.x = 10

这里 newPoint.x = 2; point.x = 10



CGPoint point = CGPointMake(2, 4);
NSValue *val = [NSValue valueWithPointer:&point];
point.x = 10;
CGPoint *newPoint = [val pointerValue];

here newPoint.x = 10; point.x = 10

这里 newPoint.x = 10; point.x = 10

回答by Sk Rejabul

In Swift, you can change a value like this:

在 Swift 中,您可以像这样更改一个值:

    var pointValueare = CGPointMake(30,30)
    NSValue(CGPoint: pointValueare)

回答by Antoine

In Swift the static method is change to an initialiser method:

在 Swift 中,静态方法更改为初始化方法:

var pointValue = CGPointMake(10,10)
NSValue(CGPoint: pointValue)

回答by user738872

&(cgpoint) -> get a reference (address) to cgpoint (NSPoint *)&(cgpoint) -> casts that reference to an NSPoint pointer *(NSPoint )(cgpoint) -> dereferences that NSPoint pointer to return an NSPoint to make the return type happy

&(cgpoint) -> 获取对 cgpoint (NSPoint *)&(cgpoint) 的引用(地址) -> 将该引用转换为 NSPoint 指针 *(NSPoint )(cgpoint) -> 取消引用该 NSPoint 指针以返回一个 NSPoint返回类型快乐

回答by Guillaume Algis

If you are using a recent-ish version of Xcode (post 2015), you can adopt the modern Objective-C syntax for this. You just need to wrap your CGPoint in @():

如果您使用的是最新版本的 Xcode(2015 年之后),您可以为此采用现代 Objective-C 语法。您只需要将 CGPoint 包装在@()

CGPoint primitivePoint = CGPointMake(4, 6);
NSValue *wrappedPoint = @(primitivePoint);

Under the hood the compiler will call +[NSValue valueWithCGPoint:]for you.

在引擎盖下,编译器会调用+[NSValue valueWithCGPoint:]你。

https://developer.apple.com/library/archive/releasenotes/ObjectiveC/ModernizationObjC/AdoptingModernObjective-C/AdoptingModernObjective-C.html

https://developer.apple.com/library/archive/releasenotes/ObjectiveC/ModernizationObjC/AdoptingModernObjective-C/AdoptingModernObjective-C.html

回答by Marc Charbonneau

Don't think of it as "converting" your point-- NSValue is a wrapper class that holds primitive structs like NSPoint. Anyway, here's the function you need. It's part of Cocoa, but not Cocoa Touch. You can add the entire function to your project, or just do the same conversion wherever you need it.

不要将其视为“转换”您的观点——NSValue 是一个包装类,它包含像 NSPoint 这样的原始结构。不管怎样,这就是你需要的功能。它是 Cocoa 的一部分,但不是 Cocoa Touch。您可以将整个函数添加到您的项目中,或者在您需要的任何地方进行相同的转换。

NSPoint NSPointFromCGPoint(CGPoint cgpoint) {
   return (*(NSPoint *)&(cgpoint));
}