iOS開發——封裝自己的下拉式功能表

來源:互聯網
上載者:User

iOS開發——封裝自己的下拉式功能表

思路1、圖片的展開:
UIImage *image = [UIImage imageNamed:@popover_background];        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(24, 0, 24, 0) resizingMode:UIImageResizingModeStretch];

-resizableImageWithCapInsets:resizingMode 方法的第二個參數,調整模式分為兩種,平鋪與展開。這裡採用展開,可以保證背景的帶箭頭的框框按照內容大小正確地將其包圍。

2、圓形過渡動畫

具體實現方式可以參考這篇部落格: iOS開發——圓形過渡動畫。這裡把代碼擺上

    CGRect originalRect = CGRectMake(point.x, point.y, 1, 1);    self.startPath = [UIBezierPath bezierPathWithOvalInRect:originalRect];    CGPoint extremePoint = CGPointMake(point.x, point.y + contentView.frame.size.height);    CGFloat radius = sqrtf(extremePoint.x * extremePoint.x + extremePoint.y * extremePoint.y);    UIBezierPath *endPath = [UIBezierPath bezierPathWithOvalInRect:                             CGRectInset(originalRect, -radius, -radius)];    self.shapeLayer = [CAShapeLayer layer];    self.shapeLayer.path = endPath.CGPath;    self.layer.mask = self.shapeLayer;    if (animated) {        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@path];        animation.fromValue = (__bridge id)(self.startPath.CGPath);        animation.toValue = (__bridge id)(endPath.CGPath);        animation.duration = 0.3f;        [self.shapeLayer addAnimation:animation forKey:@begin];    }
3、其他

①view的層級關係:

主要有兩個控制項,contentView為使用者傳入的view,一般是一個表給,使用者需要自己規定這個contentView的寬高。
另一個是imageView充當背景圖片,同時該imageView會作為使用者傳入的contentView的容器,
整個控制項為一個View,背景透明,做mask用,當菜單處於開啟狀態時使用者點擊背景會使菜單收回。
當使用者傳入contentView後,重設其原點,同時更新imageView的大小使其將整個contentView包圍。
最後,將這個整體加到window中,保證菜單處於最上層。

②座標系轉換
由使用者傳入一個控制項來決定該菜單顯示在哪時,需要用到座標系轉換。關於座標系轉換,之前的部落格中也有記錄,在此留一個簡單的結論:
[viewA convertRect:viewA.bounds toView:viewB]—得到viewA在viewB座標系下的bounds。

③CAAnimationDelegate中區分多個animation對象

首先在添加animation時為其設定標誌key:
[self.shapeLayer addAnimation:animation forKey:@end];
在代理方法中根據layer的方法拿:

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {    if (anim == [self.shapeLayer animationForKey:@end]) {        self.shapeLayer.path = self.startPath.CGPath;        [self removeFromSuperview];    }}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.