2012-05-24 39 views
2

我是iOS開發人員的初學者,我正在爲我的學校開發一個側面項目。我想要做的是有一個小圖像,用戶可以從中拖出更多圖像。基本上我有一個應用程序,我正在生成廣場,我需要有一個集合廣場,用戶可以觸摸廣場並將新廣場拖出到廣告牌上。我已經實現了這一點,但它不是所需的行爲。目前,用戶必須先觸摸按鈕然後釋放,然後才能與新方塊進行交互以將其拖離按鈕。我想讓觸摸事件以某種方式傳遞給創建的新UIImageView對象,以便無縫地拖出新的正方形。以下是我目前的代碼。從靜止的UIImageView或按鈕拖出新的UIImageView對象

//UIViewController 
#import <UIKit/UIKit.h> 
#import "Tile.h" 

@interface MenuViewController : UIViewController 
{ 
    Tile *tileObject; 
} 

@end 

#import "MenuViewController.h" 

@implementation MenuViewController 

- (IBAction)createTile:(UIButton *)sender { 
    CGFloat tileX = sender.center.x; 
    CGFloat tileY = sender.center.y; 
    tileObject = [[Tile alloc]initWithX:tileX andY:tileY]; 
    [self.view addSubview:tileObject]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return YES; 
} 

@end 




//Custom Tile Class 
#import <UIKit/UIKit.h> 

@interface Tile : UIImageView 
{ 
    CGPoint startLocation; 
} 

- (id)init; //Default Initialization 
- (id)initWithX:(CGFloat)x andY:(CGFloat)y; //Initialization with coordinate points from single screen tap 

@end 

#import "Tile.h" 

@implementation Tile 

//Default initialization 
-(id) init 
{ 
    self = [self initWithX:0 andY:0]; 
    return self; 
} 

//Initialization with coordinate points from single screen tap 
- (id)initWithX:(CGFloat)centerX andY:(CGFloat)centerY 
{ 
    //Creates a UIImageView object from an image file 
    UIImage *image = [UIImage imageNamed:@"redblock.png"]; 
    self = [super initWithImage:image]; 
    //Used to center the image under the single screen tap 
    centerX -= (image.size.height/2); 
    centerY -= (image.size.height/2); 
    //Sets the position of the image 
    self.frame = CGRectMake(centerX, centerY, image.size.width, image.size.height); 
    self.userInteractionEnabled = YES; //required for interacting with UIImageViews 
    return self; 
} 

/* Methods from Stack Overflow 
http://stackoverflow.com/questions/1991899/uiimage-detecting-touch-and-dragging 
Referenced from http://www.iphoneexamples.com/ 
*/ 
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    // Retrieve the touch point 
    CGPoint point = [[touches anyObject] locationInView:self]; 
    startLocation = point; 
    [[self superview] bringSubviewToFront:self]; 
} 
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { 
    // Move relative to the original touch point 
    CGPoint point = [[touches anyObject] locationInView:self]; 
    CGRect frame = [self frame]; 
    frame.origin.x += point.x - startLocation.x; 
    frame.origin.y += point.y - startLocation.y; 
    [self setFrame:frame]; 
} 
/* 
end of methods from Stack Overflow 
*/ 

@end 
+0

您是否想要按鈕(或者最終用於初始觸摸的任何東西)看起來與拖動的圖像相同,還是需要不同? – rdelmar

回答

2

如果初始着落點可以是圖像視圖,並且想要拖動圖像作爲其圖像,則可以這樣做:

在您的menuViewController類,把這個在viewDidLoad中(和刪除按鈕和它的方法):

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    tileObject = [[Tile alloc]initWithX:160 andY:200]; 
    [self.view addSubview:tileObject]; 
} 

然後在你的瓷磚類,在的touchesBegan:創建一個新的分片方式,並進行拖動像你一樣:withEvent之前:

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    Tile *newTile = [[Tile alloc]initWithX:160 andY:200]; 
    [self.superview addSubview:newTile]; 

    // Retrieve the touch point 
    CGPoint point = [[touches anyObject] locationInView:self]; 
    startLocation = point; 
    [[self superview] bringSubviewToFront:self]; 
} 
2

而不是使用一個按鈕,觸下事件的,您需要:

1)檢測的touchesBegan並創建瓷磚

2)檢測touchesMoved和移動你的瓷磚

+0

對不起,我有點困惑。你說這個按鈕應該是另一個處理touchesBegan和touchesMoved的UIImageView。我有一個Tile類的UIImageView子類,處理這些觸摸。我如何獲得原始按鈕或UIImageView來處理這些事件,然後將touch mid事件傳遞給具有相同觸摸事件的新創建的UIImageView? – Lbatson

+0

新創建的圖塊不需要處理任何觸摸事件,直到您稍後想要移動它爲止。所以你不需要將觸摸傳播到新的瓦片。您只需使用以原始按鈕/圖像觸摸開始的圖像,即可首次將圖塊拖出圖塊。 – ader