将图像粘贴到另一个图像

时间:2014-06-28 15:50:20

标签: ios objective-c

我目前有一些代码可以在随机位置创建一个UIImageView,它在屏幕底部生成动画。当该对象与用户控制的另一个对象发生碰撞时,会检测到碰撞。但是,我不知道如何使随机图像在碰撞后保持在用户控制的图像之上,以便随机图像与用户控制的图像一起移动。这是我到目前为止的代码:

-(void)spawnMechanism{
    timeSinceLastSpawn++;


    if(timeSinceLastSpawn >= 3)
    {
        if( arc4random() % 10 < 7 ){


            UIImageView *spawnGood;

            spawnGood=[[UIImageView alloc]initWithFrame:CGRectMake(arc4random() % 700, -100, 70,70)];
            UIImage *image;
            image=[UIImage imageNamed:@"friendly-01"];
            [spawnGood setImage:image];
            [self.view addSubview:spawnGood];

            NSLog(@"spawnGood spawned");
            [UIView animateWithDuration:2.0
                                  delay: 0.0
                                options: UIViewAnimationOptionCurveLinear
                             animations:^{
                                 CGRect frame1 =[spawnGood frame];

                                 frame1.origin.y =frame1.origin.y+950;
                                 [spawnGood setFrame:frame1];

                             }
                             completion:^(BOOL finished){

                                 if(CGRectIntersectsRect(userToken.frame, spawnGood.frame)){
                                     NSLog(@"Good Collision");

                                 }else{
                                     [UIView animateWithDuration:2.0 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
                                         CGRect frame1 =[spawnGood frame];

                                         frame1.origin.y =frame1.origin.y+950;
                                         [spawnGood setFrame:frame1];
                                     }
                                                      completion:nil];

                                     };
                             }

             ];}









            }





        }

要控制用户图像,我有以下代码:

- (IBAction)right {
    /*[UIView animateWithDuration:0.10f
                     animations:^{
                         _userToken.frame = CGRectMake(_userToken.frame.origin.x + 80, _userToken.frame.origin.y, _userToken.frame.size.width, _userToken.frame.size.height);
                     }];
}*/
    if(goRight==nil){
    goRight=[NSTimer scheduledTimerWithTimeInterval:rate
                                            target:self
                                          selector:@selector(goRight)
                                          userInfo:nil
                                           repeats:YES];
    }

        [self myDetectCollisionsOnRight];
}

- (IBAction)left{
    /*[UIView animateWithDuration:0.10f
                     animations:^{
                         _userToken.frame = CGRectMake(_userToken.frame.origin.x - 80, _userToken.frame.origin.y, _userToken.frame.size.width, _userToken.frame.size.height);
                     }];
}*/
    if(goLeft==nil){
        goLeft=[NSTimer scheduledTimerWithTimeInterval:rate
                                           target:self
                                         selector:@selector(goLeft)
                                         userInfo:nil
                                          repeats:YES];
    }
        [self myDetectCollisions];
}

-(IBAction)stopLeft{
    [goLeft invalidate];
    goLeft=nil;
}


-(IBAction)stopRight{
    [goRight invalidate];
    goRight=nil;
}

-(void)goLeft{
    userToken.center =CGPointMake(userToken.center.x -25, userToken.center.y);
    [self myDetectCollisions];


}

-(void)goRight{
    userToken.center =CGPointMake(userToken.center.x +25, userToken.center.y);
    [self myDetectCollisionsOnRight];

}

有人能够建议解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

尝试使用-addSubview:。我认为如果将其添加到userToken然后移动userToken,则spawnGood会随之移动,因为它是子视图。

if(CGRectIntersectsRect(userToken.frame, spawnGood.frame)){
     NSLog(@"Good Collision");
     [spawnGood removeFromSuperView]
     [userToken addSubview: spawnGood];
     //Position the subview where the collision took place here
}