简易拼图(OC)

本文介绍如何使用Objective-C编程语言开发一款简单的拼图游戏。从界面设计到逻辑实现,详细讲解了游戏的核心功能,包括图片切割、随机打乱和用户操作验证等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值