如何在Swift中创建UnsafeMutablePointer<CGPoint>对象

5

我需要调用UIScrollViewDelegate方法,但不知道如何创建UnsafeMutablePointer对象。

let pointer:UnsafeMutablePointer<CGPoint>  = CGPoint(x: 0, y: 1) as! UnsafeMutablePointer<CGPoint>

self.scrollViewWillEndDragging(self.collectionView, withVelocity: CGPoint(x: 0, y: 1), targetContentOffset: pointer)

func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) 
{  
     //Some code here
}
1个回答

8
import Foundation
var point = CGPoint(x: 10.0, y: 20.0)
let p = withUnsafeMutablePointer(&point) { (p) -> UnsafeMutablePointer<CGPoint> in
    return p
}

print(p.memory.x, p.memory.y) // 10.0 20.0
point.x = 100.0
print(p.memory.x, p.memory.y) // 100.0 20.0

使用Swift的语法糖来帮助。
import Foundation
var point = CGPoint(x: 10.0, y: 20.0)
let p = withUnsafeMutablePointer(&point) { $0 }

print(p.dynamicType) // UnsafeMutablePointer<CGPoint>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接