【问题标题】:How to create UnsafeMutablePointer<CGPoint> Object in swift如何快速创建 UnsafeMutablePointer<CGPoint> 对象
【发布时间】:2016-05-20 11:03:25
【问题描述】:

我需要调用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
}

【问题讨论】:

    标签: ios xcode swift scrollview


    【解决方案1】:
    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>
    

    【讨论】:

      猜你喜欢
      • 2015-09-11
      • 2020-08-20
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 2011-09-28
      相关资源
      最近更新 更多