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];
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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。