문제

All in all I am trying to get a result when one image touches another image. That is not the problem I am having though. I can't get the UIImageView to even move when I click on it and drag. Below is the code I am using! Please let me know if you can help me! Thanks!

.h

{
    IBOutlet UIImageView *image1;
    IBOutlet UIImageView *image2;
}

@property (nonatomic, retain) UIImageView *image1;
@property (nonatomic, retain) UIImageView *image2;

-(void)collision;

.m

@synthesize image1, image2;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch1 = [touches anyObject];

    [self collision];

    if ([touch1 view] != self.image1) {

        return;
    }

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch1 = [touches anyObject];

    // If the touch was in the placardView, move the placardView to its location.
    if ([touch1 view] == self.image1) {
        CGPoint location = [touch1 locationInView:image1];
        self.image1.center = location;
        return;
    }
}

-(void)collision
{
    if (CGRectIntersectsRect(image1.frame, image2.frame)) {
        NSLog(@"Touched");
    }
}

** I was able to get the code to work where I am able to move the UIImageView.

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch1 = [[event allTouches] anyObject];

    image1.center = [touch1 locationInView:self.view];


    return;
}

The only problem with this is that where ever I touch on the screen the image moves to that location. How may I have it where you have to touch the actual UIImageView to move it? Thanks!!

도움이 되었습니까?

해결책

UIImageView default userInteraction is false, so u are just enable userInteraction is True for both image view.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top