簡易拼圖(OC)

這裏寫圖片描述

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];

}

-(void)createUI
{
    //創建拼圖背景
    UIView* backView=[[UIView alloc]initWithFrame:CGRectMake(10, 60, 300, 300)];
    backView.backgroundColor=[UIColor whiteColor];
    backView.tag=100;
    [self.view addSubview:backView];
    //創建提示圖片
    UIImageView* imageView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 380, 80, 80)];
    UIImage* image1=[UIImage imageNamed:@"king2.png"];
    imageView.image=image1;
    [self.view addSubview:imageView];

    //求出切割圖片的長寬,切成9快
    CGFloat weigth=  image1.size.width/3;
    CGFloat heigth=image1.size.height/3;
    NSMutableArray* imageArr=[[NSMutableArray alloc]init];
    for (int i=0; i<3; i++) {
        for (int j=0; j<3; j++) {
            UIImage* cutImage=[self cutMyImageWithImage:image1 inrect:CGRectMake(j*weigth+1, i*heigth+1, weigth-2, heigth-2)];
            [imageArr addObject:cutImage];
        }
    }
    int sum=9;
    for (int i=0; i<3; i++) {
        for (int j=0; j<3; j++) {
            UIImageView* childView=[[UIImageView alloc]initWithFrame:CGRectMake(j*weigth+1, i*heigth+1, weigth-2, heigth-2)];

            childView.backgroundColor=[UIColor colorWithRed:(arc4random()%10+1)/10.0 green:(arc4random()%10+1)/10.0 blue:(arc4random()%10+1)/10.0 alpha:1];
            //切圖片的方法
            //以每個childView.frame來切割大圖
            //            UIImage* cutImage=[self cutMyImageWithImage:image1 inrect:childView.frame];
            //            childView.image=cutImage;

            if (i*j==4) {
                childView.image=nil;
                childView.tag=998;

            }else{
                int a=arc4random()%(sum-1);
                UIImage* tempImage=imageArr[a];
                childView.image=imageArr[a];
                [imageArr removeObject:tempImage];
                sum--;
            }

            //給每一小圖添加手勢識別器
            childView.userInteractionEnabled=YES;
            [self addTap:childView];
            [backView addSubview:childView];
        }
    }
}

-(void)addTap:(UIImageView*)childView
{
    UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc]init];
    [tap addTarget:self action:@selector(tapAction:)];
    [childView addGestureRecognizer:tap];
}

-(void)tapAction:(UITapGestureRecognizer*)tap
{
    NSLog(@"%@",NSStringFromCGRect(tap.view.frame)  );
    //UIView* backView=[self.view viewWithTag:100];
    UIImageView *nilChildView=(UIImageView*)[self .view viewWithTag:998];
    UIImageView* childView=(UIImageView*)tap.view;
    if (((int)fabs(childView.frame.origin.x-nilChildView.frame.origin.x)==100|| (int)fabs(childView.frame.origin.y-nilChildView.frame.origin.y)==100)&&(!((int)fabs(childView.frame.origin.x-nilChildView.frame.origin.x)>=100&& (int)fabs(childView.frame.origin.y-nilChildView.frame.origin.y)>=100))) {
        CGRect childRect=childView.frame;
        CGRect  nilRect=nilChildView.frame;
        [UIView animateWithDuration:0.2 animations:^{
            childView.frame=nilRect;
            nilChildView.frame=childRect;
        }];

    }
};

-(UIImage*)cutMyImageWithImage:(UIImage*)_image  inrect:(CGRect)_rect
{
    //在一張大圖裏切除一張——rect區間的小圖
    CGImageRef cgImage=  CGImageCreateWithImageInRect(_image.CGImage, _rect);
    //
    UIImage* reImage=[UIImage imageWithCGImage:cgImage ];
    return  reImage;
}

#pragma mark- 實現多個視圖上的手勢識別器響應同一個方法
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    return  YES;
}

@end
發佈了48 篇原創文章 · 獲贊 1 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章