UIImageView的使用

//创建imageView

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 280)];

    imgView.backgroundColor = [UIColor redColor];

    //设置显示的图片,默认后缀名是png

    UIImage *image = [UIImage imageNamed:@"1"];

    imgView.image = image;

    

    //设置高亮状态下显示的图片

    imgView.highlightedImage = [UIImage imageNamed:@"scene1.jpg"];

//    imgView.highlighted = YES;

    

    //边框加工

    imgView.layer.borderWidth = 1;

    imgView.layer.borderColor = [UIColor greenColor].CGColor;

    imgView.layer.cornerRadius = 5;

    

    //设置图片的填充模式UIViewContentModeScaleAspectFit:图片等比例拉伸

    imgView.contentMode = UIViewContentModeScaleAspectFit;

    

    [_window addSubview:imgView];

    

    

//    imgView.animationImages =

    

    NSMutableArray *imgArray = [[NSMutableArray alloc] init];

    

    for (int i=1; i<22; i++) {

        NSString *imgName = [NSString stringWithFormat:@"%d.jpg",i];

        UIImage *img = [UIImage imageNamed:imgName];

        

        //设置UIImage的图片拉伸点

        //img resizableImageWithCapInsets:UIEdgeInsetsMake(距上, 距左, 距下, 距右);

        

        [imgArray addObject:img];

    }

    

    //设置imgView的动画集合

    imgView.animationImages = imgArray;

    //设置动画的时间

    imgView.animationDuration = 5;

    

    //开始动画

    [imgView startAnimating];

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];

    button.frame = CGRectMake(20, 20, 20, 20);

    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];

    [imgView addSubview:button];

    

    //注意,在UIImageView添加点击事件时一定要改为yes

    imgView.userInteractionEnabled = YES;

    

    

    

    

    

    

    return YES;

}


- (void)buttonAction {


    NSLog(@"按钮被点击了");

}


猜你喜欢

转载自blog.csdn.net/Remember29/article/details/45216837