iOS開發的一些小技巧

來源:互聯網
上載者:User

標籤:

1.神器計算圖片位置的函數:AVMakeRectWithAspectRatioInsideRect()

通過這個函數,我們可以計算一個圖片放在另一個 view 按照一定的比例置中顯示,可能說的我比較抽象,還是用圖來顯示,可以說它可以直接一個 image 以任何的比例顯示顯示在 imageview 中置中所處的位置,拿 UIViewContontAspectFit來示範,

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 300, 300)];imageView.center = self.view.center;imageView.backgroundColor = [UIColor redColor];imageView.contentMode = UIViewContentModeScaleAspectFit;UIImage *image = [UIImage imageNamed:@"mm.jpg"];imageView.image = image; CGRect iamgeAspectRect = AVMakeRectWithAspectRatioInsideRect(image.size, imageView.bounds);NSLog(@"iamgeAspectRect = %@, imageView =%@",NSStringFromCGRect(iamgeAspectRect),NSStringFromCGRect(imageView.frame));[self.view addSubview:imageView];

  

2.關於 如果一個矩形如果做了平移旋轉縮放等一系列操作之後,上下左右的四個點(甚至矩形上任意一個點)的位置。

CGPoint originalCenter = CGPointApplyAffineTransform(_mStyleLeftEyeView.center,                                                     CGAffineTransformInvert(_mStyleLeftEyeView.transform));//1左眼內眼角CGPoint bottomRight = originalCenter;bottomRight.x += _mStyleLeftEyeView.bounds.size.width / 2;bottomRight.y += _mStyleLeftEyeView.bounds.size.height / 2;bottomRight = CGPointApplyAffineTransform(bottomRight, _mStyleLeftEyeView.transform);

3.在使用 pinch 的時候我們設定 pinch 縮放的最大值和最小值(系統預設沒有提供最大值和最小值的 api),設定 pinch的 maxValue,minValue.

    if([gestureRecognizer state] == UIGestureRecognizerStateBegan)    {        // Reset the last scale, necessary if there are multiple objects with different scales        mLastScale = [gestureRecognizer scale];    }    if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||        [gestureRecognizer state] == UIGestureRecognizerStateChanged)    {        CGFloat currentScale = gestureRecognizer.view.transform.a;           //計算出 縮放平移的 scale        CGFloat deletaScale = (mLastScale - [gestureRecognizer scale]);        CGFloat newScale = 1 -  deletaScale;        newScale = MIN(newScale, kMaxScale / currentScale);        newScale = MAX(newScale, kMinScale / currentScale);          CGAffineTransform scaleTransform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);        //隨著移動要調整一下 view 的 center point 位置        [gestureRecognizer view].transform = scaleTransform;        NSLog(@"self.iconView.height = %@ ,width = %@",@(self.iconView.width),@(self.iconView.height));        mLastScale = [gestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call

聰明的小夥伴應該一下就知道這個是怎麼處理的,唯一需要注意的是 當前的縮放的 scale,最初查的資料是通過

 CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];

4.最後再分享給大家一個縮放時,縮放的中心點的問題,絕大部分我們縮放都是以 view 的中心點來縮放的,但是某些情況下我們需要以下面的邊不動。譬如下面映像這種

這種方式,我最早是希望通過縮放的時候同時平移就可以處理了,根據縮放的尺寸,縮放到上面多少就平移下來多少,保持下邊不動,但是發現特別麻煩。後來使用 layer 的 anchorPoint 來出來,發現特別簡單,唯一需要填的坑就是改變 anchorPoint 的時候,它的 frame 會發生瞬移的變化,天啊嚕,還好我完美解決!

-(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;}

通過這種方式設定 anchorPoint,如果後續你做平移前 速度把 AnchorPoint設定到(0.5,0.5)的位置,就沒有問題了。

原文連結: http://www.cocoachina.com/ios/20150427/11678.html

iOS開發的一些小技巧

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.