我在一个视图中有 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
非常感谢任何帮助。