2012-11-07 18 views
0

我在視圖中有2個圖像,並且我對圖像應用了拖動觸摸功能,當圖像與視圖頂部的圖像碰撞時,它會提供警報和更改視圖頂部的圖像轉換爲不同的圖像。獲取2個圖像在視圖中拖動Xcode

我的問題是讓我的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 

enter image description here

感謝任何幫助是非常讚賞。

回答

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也看看拖動&可調整大小的視圖類,這可能有點工作節省您還