Skip to content

Instantly share code, notes, and snippets.

@JeOam
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeOam/80eac4bc5a9a17b66da5 to your computer and use it in GitHub Desktop.
Save JeOam/80eac4bc5a9a17b66da5 to your computer and use it in GitHub Desktop.
UIImageView template
// .h
@property (strong, nonatomic) IBOutlet UIImageView *logoImageView;

// .m
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x / 2, self.view.frame.origin.y / 2, 32, 32)];
    [self.logoImageView setImage:[UIImage imageNamed:@"logo"]];
    CALayer *lay = self.logoImageView.layer;

    // 发挥作用的两个设置
    [lay setMasksToBounds:YES];
    [lay setCornerRadius:16]; // 正方形边长的一半

    [self.view addSubview:self.logoImageView];
}
@JeOam
Copy link
Author

JeOam commented Aug 7, 2014

UITapGestureRecognizer

UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
[imageView addGestureRecognizer:tap];
imageView.userInteractionEnabled = YES;


- (void) tap:(UITapGestureRecognizer *) tap{
  NSLog(@"tap.view.tag is %d", tap.view.tag);
}

@JeOam
Copy link
Author

JeOam commented Aug 19, 2014

每个UIView对象都有一个layer属性,这个layer属性实际上是一个CALayer对象。这个对象负责把UIView对象显示到屏幕上。而layer则有一个cornerRadius属性,用来指定layer的边框圆角。但是光设定这个属性是不够的,因为layer默认会把图像超出边框的部分也显示出来,所以必须指定masksToBounds属性为YES,让layer切掉边框外的部分。最终实现了图像的圆角显示。

via here

@JeOam
Copy link
Author

JeOam commented Aug 22, 2014

图片拉伸:resizableImageWithCapInsets

// Creates and returns a new image object with the specified cap insets.
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets

其中Insets这个参数的格式是(top,left,bottom,right),从上、左、下、右分别在图片上画了一道线,这样就给一个图片加了一个框。只有在框里面的部分才会被拉伸,而框外面的部分则不会改变。

via here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment