2011-03-07 30 views
0

嗨,我是iPhone開發新手。
我以編程方式在按鈕單擊上創建UIImageViews。但每次我點擊按鈕,他們都會在同一個地方畫出來。圖像繪製代碼如下。檢查顯示在UIScrollview的圖像

- (IBAction)Button 
{ 
arrayOfImages = [[NSMutableArray alloc]init]; 
float x = 15.0,y = 15.0, width = 100.0, height = 100; 
CGRect frame = CGRectMake(x, y, width, height); 
int i= 0; 

if ([shape isEqualToString:@"Rectangle"]) 
{ 

    UIGraphicsBeginImageContext(frame.size); 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetLineWidth(context, 2.0);  
    CGRect main =CGRectMake(x, y, 70.0, 30.0); 
    CGContextFillRect(context, main); 

    CGRect topSeat1 = CGRectMake(15.0, 0.0, 15.0, 13.0); 
    CGRect topSeat2 = CGRectMake(42.5, 0.0, 15.0, 13.0); 
    CGRect topSeat3 = CGRectMake(70.0, 0.0, 15.0, 13.0); 

    CGRect leftSeat = CGRectMake(0.0, 22.5, 13.0, 15.0); 

    CGRect rightSeat = CGRectMake(87.0, 22.5, 13.0, 15.0); 


    [[UIColor redColor]set]; 
    //UIRectFill(main); 
    UIRectFill(topSeat1); 
    UIRectFill(topSeat2); 
    UIRectFill(topSeat3); 
    UIRectFill(leftSeat); 
    UIRectFill(rightSeat); 
    UIRectFrame(main); 

    [[UIColor blackColor]set]; 
    UIRectFrame(topSeat1); 
    UIRectFrame(topSeat2); 
    UIRectFrame(topSeat3); 
    UIRectFrame(leftSeat); 
    UIRectFrame(rightSeat); 

    UIImage * images = [[UIImage alloc]init]; 
    images = UIGraphicsGetImageFromCurrentImageContext(); 

    UIImageView* myImage=[[UIImageView alloc]initWithFrame:frame]; 
    [myImage setImage:images]; 
    myImage.tag= i; 
    i++; 


    UIGraphicsEndImageContext(); 
    [self.view addSubview:myImage]; 
    [arrayOfImages addObject:myImage]; 
    [myImage setUserInteractionEnabled:YES]; 
} 
} 

所有我想的是,如果我點擊它應檢查是否有已經UIImageView的在那個地方畫的按鈕,然後它繪製的UIImageView有點遠離它。
我將不勝感激,如果你幫我一些代碼。

回答

2

使用一個變量來存儲按鈕的點擊次數,然後使用變量來調整您的視角的地方

,或者你可能會問了SuperView已經子視圖,並與您的圖像進行比較的子視圖的框架查看

for (UIView *subview in self.view.subviews) { 
    if (/* compare subview.frame with myImage.frame */) { 
     /* update myImage.frame */ 
     break; 
    } 
} 
+0

因爲它依賴於用戶,然後他什麼時候按一下按鈕,如何將我能調整我的runtime.If的UIImageViews的地方ü可以給我這樣的代碼什麼的一些例子。 – Hanny 2011-03-07 11:10:33

+0

@Hanny:我已經編輯了答案 – 2011-03-08 22:28:20