疯狂猜图中的图片放大缩小功能的实现

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *questionImageBtn;
@property (strong, nonatomic) UIButton *mask;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//解决状态栏黑掉的方法
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}


- (UIButton *)mask
{
    if (_mask == nil) {
        //1,新增一个蒙版
        _mask = [[UIButton alloc] initWithFrame:self.view.bounds];
        //背景色及其透明度
        _mask.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.5];//0.5是颜色的透明度
        _mask.alpha = 0;
        [_mask addTarget:self action:@selector(smallImage) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_mask];
    }
    return _mask;
}
//放大或者缩小图片
- (IBAction)bigImage:(id)sender {
    
    if (self.mask.alpha == 0) {
        //放大图片
        
        //1,显示蒙版
        self.mask.alpha = 1;
    
        //2,将图片放到最外层
        [self.view bringSubviewToFront:self.questionImageBtn];
    
        //3,放大图片
    
        NSInteger bigImageWith = self.view.bounds.size.width;
        NSInteger bigImageHeight = bigImageWith;
    
    
        NSInteger bitImageX = 0;
    
        NSInteger bigImageY = (self.view.bounds.size.height-bigImageHeight)*0.5;
    
        
        [UIView animateWithDuration:1 animations:^{
            self.questionImageBtn.frame = CGRectMake(bitImageX, bigImageY, bigImageWith, bigImageHeight);
        }];
    }else{
        //缩小图片
        [self smallImage];
    
    }
    
}

- (void)smallImage
{
    [UIView animateWithDuration:1 animations:^{
        //1,图片回到初始状态
        self.questionImageBtn.frame = CGRectMake(85, 98, 150, 150);
    } completion:^(BOOL finished) {
        //2,蒙版透明度变为0
        [UIView animateWithDuration:1 animations:^{
             self.mask.alpha = 0;
        }];
    }];
}

@end


你可能感兴趣的:(疯狂猜图中的图片放大缩小功能的实现)