ios 小结 scrollView 与 UIEdgeInsets

来源:互联网 发布:mac os x 10.10 ios 编辑:程序博客网 时间:2024/05/29 12:30

因为公司的 ios 人员走光了。。于是我就转去做ios了。从来没有接触过ios。刚开始还是很困难。一直忙着 一边学习。一边赶项目。没有时间来总结一下。终于完成了项目的一个阶段。 有时间总结了。回过头 看着以前刚学习的笔记。 又觉的太幼稚了。。不知道写些什么好。 想来想去 就随便写写工作中遇到的问题。已经花了一点时间才弄明白的东西吧。

首先就是这个UIEdgeInsets 对于这个属性。刚开始接触的时候就有点蒙。特别是看到负数 一开始完全不能理解。 资料又少。自己一个人摸索。现在有点理解了。也不知道对不对 记下来吧。  

如果都是很正常的数字。 上下左右 插入个几个像素的边距。都是很容易理解的。就像是塞进去一块砖头是那么大 撑着体积。一开始不能理解的是负数。现在想想其实就好像塞砖头改变了里面内容frame的尺寸 但是不改变本身的frame。比如UIEdgeInsets 的left 设置为 100 就是 。就好像在左边塞进来一个100长度的砖头。这样内容frame 的left -100 。width 剪掉了100. left 设置成 -100 就是内容内容frame的left -100. width +100;当然 并没有实际改变frame 只是draw 绘制的位置变成了而已。出了frame了之后 就遮挡了。  这样就解释了之前我遇到的一个情况 就是。对一个width为100的空间UIEdgeInsets  right 设置为-100 界面看上去的效果是 向右偏移了50像素。其实原因是 绘制的尺寸变成了100+100 .因为显示的width 还是100, 是居中显示的。所以显示出来就是偏移了50.


在一些需求上面 在scroolView 跟 UIEdgeInsets 配合起来使用 有奇效。 在scroolView上面使用 UIEdgeInsets。对于我这个新手来说 更加的困难。在加上边距之后 。界面上表现出来的结果就像是改变了ContentSize 的值一样。UIEdgeInsets 的left 为100的时候 就是在 content左边插入了 width为100的砖头。导致了 ContentSize 在界面上表现出来的长度别你定义的大了100. contentOffset.x 也可以滚动到-100去了。 如果是在不需要放大缩小的界面上使用。感觉不是很有必要。真正让我觉的有奇效的是在放大缩小功能上面取了。

我的项目有个自定义的裁剪头像 我就是在这里用到了这个。感觉很好用。原因是 当uiscroll zoom 后 他的ContentSize 会被系统重新设置。你定义的会失效了。会变成你放大返回的view的尺寸*缩放比例  这个时候很难通过改变contentOffset 的值 来给图片定位(或者动态改变ContentSize 来实现?)。而这时候 UIEdgeInsets 是一直存在不会消失的。并且数值也不说缩放比例变化而变化。这时候 就容易实现在裁剪框里面剪切图片并且放大拖动 不会离开边框了。

下面是我自定义切图view

////  MyImageCutView.h//  iGrow4//  裁剪图片//  Created by xiezhaojun_x@163.com on 14-12-10.//  Copyright (c) 2014年 iGrow. All rights reserved.//#import <UIKit/UIKit.h>@interface MyImageCutView : UIView <UIScrollViewDelegate>@property  (nonatomic,assign) CGSize cutSize;@property (nonatomic,strong) UIImage *cutImage;-(UIImage *)getCutImage;@end@interface CoverView : UIView@property  (nonatomic,assign) CGSize cutSize;@end


////  MyImageCutView.m//  iGrow4////  Created by Aurora_sgbh on 14-12-10.//  Copyright (c) 2014年 iGrow. All rights reserved.//#import "MyImageCutView.h"#import "UIView+Addition.h"#import "CommonDefine.h"#import "ColorHelper.h"#import "UIImage+KIAdditions.h"#import "ShareHelper.h"@interface MyImageCutView ()@property(nonatomic,strong) UIScrollView *scrollView;@property(nonatomic,strong) UIImageView *imageShowView;@property(nonatomic,strong) CoverView *coverView;@property(nonatomic,assign) UIEdgeInsets imageInset;@end@implementation MyImageCutView-(id)initWithCoder:(NSCoder *)aDecoder{    self =[super initWithCoder:aDecoder];    if(self){        self.cutSize = CGSizeMake(320, 320);        self.height = kDeiveHeight - 68;        self.scrollView =[[UIScrollView alloc]initWithFrame:self.bounds];//        [_scrollView setBounces:NO];//        [_scrollView setShowsHorizontalScrollIndicator:NO];//        [_scrollView setShowsVerticalScrollIndicator:NO];//        [self.scrollView setMinimumZoomScale:1];//        [self.scrollView setMaximumZoomScale: 5.0f];//        _scrollView.scrollEnabled = YES;//        _scrollView.pagingEnabled = YES;        self.scrollView.delegate = self;             self.scrollView.bounces = NO;        self.scrollView.decelerationRate =0;        self.scrollView.delegate = self;        [self addSubview:self.scrollView];                self.imageShowView =[[UIImageView alloc]init];        [self.scrollView addSubview:self.imageShowView];                self.coverView =[[CoverView alloc]initWithFrame:self.bounds];        [ self.coverView setBackgroundColor:[UIColor clearColor]];        [ self.coverView setUserInteractionEnabled:NO];        [self addSubview:self.coverView];                [self initScrollInset];        [self initCoverView];            }    return self;}-(void)setCutSize:(CGSize)cutSize{    _cutSize = cutSize;    [self initScrollInset];    [self initCoverView];}-(void) initScrollInset{    CGFloat x = (CGRectGetWidth(self.bounds) - self.cutSize.width) / 2;    CGFloat y = (CGRectGetHeight(self.bounds) -  self.cutSize.height) / 2;    CGFloat top = y;    CGFloat left = x;    CGFloat right = CGRectGetWidth(self.bounds)- self.cutSize.width - x;    CGFloat bottom = CGRectGetHeight(self.bounds)- self.cutSize.height                                                                                                                          - y;    _imageInset = UIEdgeInsetsMake(top, left, bottom, right);    [[self scrollView] setContentInset:_imageInset];        [[self scrollView] setContentOffset:CGPointMake(0, 0)];}-(void) initCoverView{    [self.coverView setCutSize:self.cutSize];}-(void)setCutImage:(UIImage *)cutImage{    _cutImage = cutImage;    [self updateZoomScale];    [self.imageShowView setImage:cutImage];}- (void)updateZoomScale {    CGFloat width = _cutImage.size.width;    CGFloat height = _cutImage.size.height;            CGFloat xScale = self.cutSize.width / width;    CGFloat yScale = self.cutSize.height / height;         CGFloat min = MAX(xScale, yScale);    [[self imageShowView] setFrame:CGRectMake(0, 0, width, height)];    self.scrollView.minimumZoomScale = min;    self.scrollView.maximumZoomScale = min*3;    [[self scrollView] setZoomScale:min animated:YES];        //让图片居中//    CGSize contentSize = self.scrollView.contentSize;//    float left = (min*width - _cutSize.width)/2-_imageInset.left;    float top = (min*height - _cutSize.height)/2 -_imageInset.top;    self.scrollView.contentOffset =CGPointMake(left,top );//    self.scrollView.contentOffset.x = ;//    self.scrollView.contentOffset.x = -(contentSize.width - _cutSize.width)/2;//    self.scrollView.contentOffset.y = -(contentSize.height - _cutSize.height)/2;}- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {    return self.imageShowView;}-(void)scrollViewDidZoom:(UIScrollView *)scrollView{    if(scrollView.zoomScale<= scrollView.minimumZoomScale){            }}-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{        NSLog(@"%f------%f",self.scrollView.contentOffset.x,self.scrollView.contentOffset.y);//    [[self imageShowView] setFrame:CGRectMake(150, 0, width, height)];//    self.scrollView.contentOffset = CGPointMake(0, -284);//    if (scrollView.zoomScale <= scrollView.minimumZoomScale) {//        self.imageShowView.center =  CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);//    }//    [self.imageView printRectLog];}//-(void)setCutImage:(UIImage *)image{//    _cutImage = image;//    [self.imageShowView setImage:image];//}-(UIImage *)getCutImage{    CGFloat zoomScale = [self scrollView].zoomScale;        CGFloat offsetX = [self scrollView].contentOffset.x;    CGFloat offsetY = [self scrollView].contentOffset.y;    CGFloat aX = offsetX+_imageInset.left;    CGFloat aY = offsetY+_imageInset.top;    aX = aX / zoomScale;    aY = aY / zoomScale;    CGFloat aWidth = self.cutSize.width/ zoomScale;//    CGFloat aHeight = self.cutSize.height/ zoomScale;//        //    imageView.transform = CGAffineTransformMakeRotation(M_PI);    UIImage *image = [self.cutImage cropImageWithX:aX y:aY width:aWidth height:aHeight];        return image;}@end#pragma mark --------coverView#define kMaskViewBorderWidth 1.0f@interface CoverView()@end@implementation CoverView-(void)setCutSize:(CGSize)cutSize{    _cutSize=cutSize;    [self setNeedsDisplay];}- (void)drawRect:(CGRect)rect {    [super drawRect:rect];        CGFloat x = (CGRectGetWidth(self.bounds) - _cutSize.width ) / 2;    CGFloat y = (CGRectGetHeight(self.bounds) - _cutSize.height) / 2;    CGRect cropRect = CGRectMake(x+(kMaskViewBorderWidth/2), y+(kMaskViewBorderWidth/2), _cutSize.width-kMaskViewBorderWidth, _cutSize.height-kMaskViewBorderWidth);    CGContextRef ctx = UIGraphicsGetCurrentContext();    CGContextSetRGBFillColor(ctx, 94, 97,99, .3);    CGContextFillRect(ctx, self.bounds);        CGContextClearRect(ctx, cropRect);        CGContextSetStrokeColorWithColor(ctx, [ColorHelper GetAlertBtnColor].CGColor);    CGContextStrokeRectWithWidth(ctx, cropRect, kMaskViewBorderWidth);    }@end



1 0
原创粉丝点击