0

我在一个视图中有 2 个图像,我对图像应用了拖动触摸功能,当图像与视图顶部的图像发生碰撞时,它会向您发出警报并将视图顶部的图像更改为不同的图像图片。

我的问题是让我的 if else 语句起作用,如果我取出 == 图像

([touch view] == image) {

只有一张图像拖动,我似乎无法同时拖动两张图像。我的 if else 语句没有错误,但似乎不起作用。

frfViewController.h

#import <UIKit/UIKit.h>

@interface frfViewController : UIViewController {
UIImageView *image;
UIImageView *image2;
UIImageView *images;
UIImageView *collisionImage;

}

@property (nonatomic, retain) IBOutlet UIImageView *image;
@property (nonatomic, retain) IBOutlet UIImageView *image2;
@property (nonatomic, retain) IBOutlet UIImageView *collisionImage;
@property (nonatomic, retain) IBOutlet UIImageView *images;
@end

frfViewController.m

#import "frfViewController.h"

@interface frfViewController ()

@end

@implementation frfViewController

@synthesize image;
@synthesize image2;
@synthesize images;
@synthesize collisionImage;

- (void)viewDidLoad
{    
}

- (void)viewWillAppear:(BOOL)animated
{

//      [self.navigationController setNavigationBarHidden:YES animated:animated];
//      [super viewWillAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:NO animated:animated];
[super viewWillDisappear:animated];
}

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];

if ([touch view] == image) {

    image.center = location;
    image.userInteractionEnabled = YES;


}

else if ([touch view] == image2) {

    image2.center = location;
    image2.userInteractionEnabled = YES;

}

[self ifCollided];
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}

-(void) ifCollided {
if (CGRectIntersectsRect(image.frame, collisionImage.frame) ||  CGRectIntersectsRect(image2.frame, collisionImage.frame)) {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"image Collided"
                                                    message:@"FuckYea"
                                                   delegate:nil
                                          cancelButtonTitle:@"Reset!"
                                          otherButtonTitles:nil];
    [alert show];

    CGRect myImageRect = CGRectMake(0, 0, 320, 100);
    images = [[UIImageView alloc] initWithFrame:myImageRect];
    [images setImage:[UIImage imageNamed:@"004.jpg"]];
    [self.view addSubview:images];
 }


}

@end

在此处输入图像描述

非常感谢任何帮助。

4

1 回答 1

0

从 iOS 4 起,您可以使用 UIPanGestureRecognizer 轻松进行拖动,

 UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@selector(labelDragged:)];
 [yourview addGestureRecognizer:gesture]; 

将识别器添加到每个 UIView 并将选择器设置为指向您设置新坐标的位置 = 您可以限制它的位置,例如,如果您遇到碰撞...

- (void)labelDragged:(UIPanGestureRecognizer *)gesture
{
UILabel *label = (UILabel *)gesture.view;
CGPoint translation = [gesture translationInView:label];

// move label , limit it to stay inside self.view.frame
float newx = label.center.x + translation.x;
float newy = label.center.y + translation.y;

if (newx<0) { newx = 0; }
if (newy<0) { newy = 0; }

if (newx>self.view.frame.size.width) {
    newx = self.view.frame.size.width;
}

if (newy>self.view.frame.size.height) {
    newy = self.view.frame.size.height;
}

//possibly compute collision here and limit new coordinates further

label.center = CGPointMake(newx,newy);

// reset translation
[gesture setTranslation:CGPointZero inView:label];
}

还可以查看https://github.com/spoletto/SPUserResizableView上的 draggable&resizable View 类,这也可以为您节省一些工作

于 2012-11-07T08:10:23.440 回答