0

ビューに2つの画像があり、画像にドラッグタッチ機能を適用しました。画像がビューの上部の画像と衝突すると、アラートが表示され、ビューの上部の画像が別の画像に変更されます。画像。

私の問題は、ifelseステートメントを機能させることです。

([touch view] == image) {

片方の画像だけがドラッグされ、両方の画像をドラッグできないようです。私のifelseステートメントにはエラーはありませんが、機能していないようです。

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 に答える