Believe in your own future, will thank yourself right now Sinner Yun

Sinner_Yun

拼图

 

 

 

 

 

 

  

 

#import "ViewController.h"

 

@interface ViewController ()

 

@end

 

@implementation ViewController

 

- (UIImage *)clipImage:(UIImage *)image inRect:(CGRect)rect

{//返回image中rect范围内的图片

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

    UIImage *subImage = [UIImage imageWithCGImage:imageRef];

    return subImage;

}

 

 

 

- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor =[UIColor cyanColor];

 

    UIImageView *iv=[[UIImageView alloc]initWithFrame:CGRectMake(8, self.view.bounds.size.height-180, 150, 150)];

    [iv setImage:[UIImage imageNamed:@"king2"]];

    [self.view addSubview:iv];

    

    UIButton *btn=[UIButton buttonWithType:UIButtonTypeSystem];

    btn.frame=CGRectMake(200, 400, 100, 50);

    [btn setTitle:@"恢复" forState:UIControlStateNormal];

    btn.backgroundColor=[UIColor orangeColor];

    [self.view addSubview:btn];

    [btn addTarget:self action:@selector(nc) forControlEvents:UIControlEventTouchUpInside];

   

    

    

    [self addpic];

    

    

    

}

-(void)nc

{

    for (int i=0; i<9; i++) {

        UIImageView *iv=(UIImageView *)[self.view viewWithTag:100+i];

        [UIImageView animateWithDuration:1 animations:^{

            iv.frame=CGRectMake((i%3)*102+2, (i/3)*102+2, 100, 100);

        }];

    }

}

 

-(void)addpic

{

    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(6, 60, 308, 308)];

    view.backgroundColor=[UIColor orangeColor];

    [self.view addSubview:view];

    

    for (int i=0; i<9; i++) {

        UIImageView *a=[[UIImageView alloc]initWithFrame:CGRectMake((i%3)*102+2, (i/3)*102+2, 100, 100)];

        a.image=[self clipImage:[UIImage imageNamed:@"king4"] inRect:CGRectMake((i%3)*100, (i/3)*100, 100, 100)];

        [view addSubview:a];

        

    }

    

    

    for (int i=0; i<9; i++) {

        UIImageView *qview=[[UIImageView alloc]initWithFrame:CGRectMake((i%3)*102+2, (i/3)*102+2, 100, 100)];

        qview.image=[self clipImage:[UIImage imageNamed:@"king2"] inRect:CGRectMake((i%3)*100, (i/3)*100, 100, 100)];

        qview.userInteractionEnabled=YES;

        qview.tag=100+i;

        

        if (i==8) {

            qview.image =  [UIImage imageNamed:@"king3"];

            

        }

        

        [view addSubview:qview];

 

 

    }

    

    

}

 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    //获取触摸视图

    UITouch *touch =[touches anyObject];

    //NSLog(@"22222");

    if ([touch.view isKindOfClass:[UIImageView class]]) {

        UIImageView *kong = (UIImageView *)[self.view viewWithTag:108];

        //NSLog(@"%@",NSStringFromCGPoint(kong));

        CGPoint temp = touch.view.center;

        if (kong.center.y == touch.view.center.y) {

            if (kong.center.x - touch.view.center.x ==102 ||kong.center.x - touch.view.center.x ==-102 ) {

                [UIView animateWithDuration:1 animations:^{

                    touch.view.center = kong.center;

                    kong.center = temp;

                                    }];

            }

        }

        if (kong.center.x == touch.view.center.x) {

            if (kong.center.y - touch.view.center.y ==102 ||kong.center.y - touch.view.center.y ==-102 ) {

                [UIView animateWithDuration:1 animations:^{

                    touch.view.center = kong.center;

                    kong.center = temp;

                }];

            }

        }

    }

    

 

}

 

posted on 2014-03-25 21:40  Sinner_Yun  阅读(243)  评论(0编辑  收藏  举报

导航