Converting a CGPoint to NSValue

后端 未结 7 1893
猫巷女王i
猫巷女王i 2020-12-17 09:42

In CABasicAnimation.fromValue I want to convert a CGPoint to a \"class\" so I used NSValue valueWithPoint but in device m

相关标签:
7条回答
  • 2020-12-17 10:24

    In Swift, you can change a value like this:

        var pointValueare = CGPointMake(30,30)
        NSValue(CGPoint: pointValueare)
    
    0 讨论(0)
  • 2020-12-17 10:25

    &(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

    0 讨论(0)
  • 2020-12-17 10:26

    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.

    NSPoint NSPointFromCGPoint(CGPoint cgpoint) {
       return (*(NSPoint *)&(cgpoint));
    }
    
    0 讨论(0)
  • 2020-12-17 10:34

    There is a UIKit addition to NSValue that defines a function

    + (NSValue *)valueWithCGPoint:(CGPoint)point
    

    See iPhone doc

    0 讨论(0)
  • 2020-12-17 10:36

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

    var pointValue = CGPointMake(10,10)
    NSValue(CGPoint: pointValue)
    
    0 讨论(0)
  • 2020-12-17 10:38

    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 @():

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

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

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

    0 讨论(0)
提交回复
热议问题