iOS開發程式猿必備技巧

帶著紅領巾的雷鋒發表於2015-05-05
   隨著移動智慧手機的火爆,作為移動智慧手機作業系統的ios也越來越發火爆,本文主要是一個潛伏在碼農界N久的iOS程式猿總結、收刮的一些ios開發精華技巧,需要的ios碼農請收藏,對於這些技巧不喜勿噴。

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];

log 打因結果如下: iamgeAspectRect = {{37.563884156729145, 0}, {224.87223168654171, 300}}, imageView ={{37.5, 183.5}, {300, 300}} 可以從 log 得出 對應的 image 以 aspectFit 的方式在 imageView 的位置,在 imageView 中的位置是(37.5,0)。這樣你根本不需要任何多的程式碼來計算了。(ps:這個函式是在 AV框架的,童鞋們自行匯入。)

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);

首先這個 styleLeftView 就是一個矩形的 view,這裡以右下角的點做示範,無論做了任何的 tranform 之後都可以得到它的點的位置。

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];

來得到的,但是不知道為什麼 layer的 transform 的 scale 和 view 的當前的縮放 scale 不一致,通過 debug,得到 view的 transform 的 a的值和當前的縮放值是一樣的。

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)的位置,就沒有問題了。

文章來源:ios開發 推薦學習:ios開發視訊教程

相關文章