第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

更改我的CALayer的anchorPoint會(huì)移動(dòng)視圖

更改我的CALayer的anchorPoint會(huì)移動(dòng)視圖

拉丁的傳說 2019-09-18 19:32:25
我想改變anchorPoint,但保持視圖在同一個(gè)地方。我試過NSLog-ing分詞self.layer.position和self.center他們都保持不變,無論更改anchorPoint的。然而我的觀點(diǎn)卻在變化關(guān)于如何做到這一點(diǎn)的任何提示?self.layer.anchorPoint = CGPointMake(0.5, 0.5);NSLog(@"center point: %f %f", self.layer.position.x, self.layer.position.y);self.layer.anchorPoint = CGPointMake(1, 1);NSLog(@"center point: %f %f", self.layer.position.x, self.layer.position.y);輸出是:2009-12-27 20:43:24.161 Type[11289:207] center point: 272.500000 242.5000002009-12-27 20:43:24.162 Type[11289:207] center point: 272.500000 242.500000
查看完整描述

3 回答

?
汪汪一只貓

TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊

Core Animation Programming Guide 的Layer Geometry and Transforms部分解釋了CALayer的position和anchorPoint屬性之間的關(guān)系。基本上,層的位置是根據(jù)圖層的anchorPoint的位置指定的。默認(rèn)情況下,圖層的anchorPoint為(0.5,0.5),位于圖層的中心。設(shè)置圖層的位置時(shí),您將在其超層圖層的坐標(biāo)系中設(shè)置圖層中心的位置。


因?yàn)槲恢檬窍鄬?duì)于圖層的anchorPoint,所以在保持相同位置的同時(shí)更改該anchorPoint會(huì)移動(dòng)圖層。為了防止這種移動(dòng),您需要調(diào)整圖層的位置以考慮新的anchorPoint。我這樣做的一種方法是抓取圖層的邊界,將邊界的寬度和高度乘以舊的和新的anchorPoint的標(biāo)準(zhǔn)化值,取兩個(gè)anchorPoints的差值,并將該差值應(yīng)用于圖層的位置。


您甚至可以通過使用CGPointApplyAffineTransform()UIView的CGAffineTransform以這種方式考慮輪換。


查看完整回答
反對(duì) 回復(fù) 2019-09-18
?
holdtom

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊

我有同樣的問題。Brad Larson的解決方案即使在視圖旋轉(zhuǎn)時(shí)也能很好地工作。這是他的解決方案翻譯成代碼。


-(void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view

{

    CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, 

                                   view.bounds.size.height * anchorPoint.y);

    CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, 

                                   view.bounds.size.height * view.layer.anchorPoint.y);


    newPoint = CGPointApplyAffineTransform(newPoint, view.transform);

    oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform);


    CGPoint position = view.layer.position;


    position.x -= oldPoint.x;

    position.x += newPoint.x;


    position.y -= oldPoint.y;

    position.y += newPoint.y;


    view.layer.position = position;

    view.layer.anchorPoint = anchorPoint;

}

而快速的等價(jià)物:


func setAnchorPoint(anchorPoint: CGPoint, forView view: UIView) {

    var newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x, view.bounds.size.height * anchorPoint.y)

    var oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x, view.bounds.size.height * view.layer.anchorPoint.y)


    newPoint = CGPointApplyAffineTransform(newPoint, view.transform)

    oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform)


    var position = view.layer.position

    position.x -= oldPoint.x

    position.x += newPoint.x


    position.y -= oldPoint.y

    position.y += newPoint.y


    view.layer.position = position

    view.layer.anchorPoint = anchorPoint

}

SWIFT 4.x


func setAnchorPoint(anchorPoint: CGPoint, forView view: UIView) {

    var newPoint = CGPoint(x: view.bounds.size.width * anchorPoint.x,

                           y: view.bounds.size.height * anchorPoint.y)



    var oldPoint = CGPoint(x: view.bounds.size.width * view.layer.anchorPoint.x,

                           y: view.bounds.size.height * view.layer.anchorPoint.y)


    newPoint = newPoint.applying(view.transform)

    oldPoint = oldPoint.applying(view.transform)


    var position = view.layer.position

    position.x -= oldPoint.x

    position.x += newPoint.x


    position.y -= oldPoint.y

    position.y += newPoint.y


    view.layer.position = position

    view.layer.anchorPoint = anchorPoint

}


查看完整回答
反對(duì) 回復(fù) 2019-09-18
  • 3 回答
  • 0 關(guān)注
  • 833 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)