iOS拼图游戏

释放双眼,带上耳机,听听看~!

//给出一张图片,然后将该图片的每块指定位置分割出来,放进数组中,然后循环摆放。点击的时候判断是否移动,简单的拼图游戏即成。

 

#define VIEW_WIDTH self.view.frame.size.width

#define VIEW_HEIGHT self.view.frame.size.height




@interface
ViewController (){

    UIImageView *_spaceImageView;

    UIImage *_image;

}




@end




@implementation ViewController




- (void)viewDidLoad {

    [superviewDidLoad];

    //创建完整图片

    [selfcreateSmallImageView];

    //创建拼图图片

    [selfcreatePuzzle];

}




- (void)createPuzzle{

    UIImage *image = [UIImageimageNamed:@"beautifulGirl"];

    CGSize imageSize = image.size;

    //改变难易程度的关键


    NSInteger cube =4;

    CGFloat width = imageSize.width / cube;

    NSMutableArray *imageList = [[NSMutableArrayalloc]init];

    //循环创建 4* 4正方形

    for (int i =0; i < cube * cube; i++) {

        //行

        int row = i / cube;

        //列

        int column = i % cube;

        CGRect rect =CGRectMake(column * width, row * width, width, width);

        //获取制定位置的切图

        UIImage *subImage = [selfclipImage:image
withRect:rect];

        [imageList addObject:subImage];

    }




    for (int i =0; i < cube * cube; i++) {

       int row = i / cube;

       int column = i % cube;

     UIImageView *subImageView = [[UIImageViewalloc]initWithFrame:CGRectMake((VIEW_WIDTH
- imageSize.width) /
2 + column * width, 50 + row * width , width, width)];

       
//取图片数组中的逆序,从而造成图片的乱序

        subImageView.image = imageList[cube * cube -1 - i];

        //允许用户交互

        subImageView.userInteractionEnabled =YES;

       
//将最后一个清空,记录空白图片视图的坐标

        if (i ==0) {

            subImageView.image =nil;

            _spaceImageView = subImageView;

        }

        //做出来分割线

        subImageView.layer.borderColor = [UIColorgrayColor].CGColor;

        subImageView.layer.borderWidth =1;

//添加点击手势

        UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapHandle:)];

        [subImageView addGestureRecognizer:tap];

        [self.viewaddSubview:subImageView];

    }

}




- (void)tapHandle:(UITapGestureRecognizer *)tap{

    if (tap.view !=_spaceImageView) {

        CGFloat tapX = tap.view.frame.origin.x;

        CGFloat tapY = tap.view.frame.origin.y;

        CGFloat spaceX =_spaceImageView.frame.origin.x;

        CGFloat spaceY =_spaceImageView.frame.origin.y;

       
//判断是否和空白图片视图交换

        if (fabs(spaceX - tapX ) +fabs(spaceY - tapY) ==
_spaceImageView.frame.size.width)
{

            CGRect frame = tap.view.frame;

            tap.view.frame =_spaceImageView.frame;

            _spaceImageView.frame = frame;

        }

    }

}

//下方显示完整的图片


- (void)createSmallImageView{

    UIImageView *downImageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"beautifulGirl"]];

    downImageView.frame =CGRectMake((VIEW_WIDTH -100)
/ 3,VIEW_HEIGHT -
200,200,
200);

    [self.viewaddSubview:downImageView];

}




//给一个图片对象,给一个需要切割的大小,将图片切割成为指定大小的小图片

- (UIImage *)clipImage:(UIImage *)image withRect:(CGRect )rect{

    CGImageRef CGImage =CGImageCreateWithImageInRect(image.CGImage, rect);

    return [UIImageimageWithCGImage:CGImage];

}

 

人已赞赏
iOS文章

Swift结构体

2020-4-7 16:48:01

iOS文章

Swift--类的使用

2020-4-8 12:16:55

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索