2013-05-06 21 views
-1

我正在開發一個應用程序,其中有一個水平滾動條。 滾動區域包含不同的顏色[只是顏色的UIImageView]。用戶干預後設置UIImageView背景色

在滾動區域上方是一個畫布[也是一個UIImageView]。

現在,我想要的是,當我在滾動區域選擇任何顏色時,它應該設置在畫布

它非常像一個顏色選擇器,但我已經提供了純色選項供用戶選擇。

我應該怎麼辦?任何示例代碼,讓我開始或我的大腦踢踢會很棒?

感謝名單一噸

回答

2

你可以把你滾動視圖內UIButtons而不是UIImageViews的顯示顏色進行挑選。

這將使實現更容易。用戶將選擇任何顏色按鈕,您將在按鈕動作選擇器方法中收到回調。然後基於按鈕標籤或任何屬性,您可以將顏色設置爲畫布。

在當前的實現中,它會很棘手,因爲您需要知道確切的內容偏移量,以確定哪個UIImageView被按下。

編碼步驟:

  • 在您的滾動視圖中添加UIButtons代替的UIImageView像:

    UIButton* button = [[UIButton alloc] initWithFrame:someRect]; 
    //put some tag to button 
    button.tag = someInt; 
    //add selector to each button to get callback 
    [view addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside]; 
    [scrollView addSubview:button]; 
    
  • 在btnSelected方法把這個代碼:

    -(IBAction)btnSelected:(id)sender; 
    { 
        UIButton* button = (UIButton*) sender; 
    
        if (button.tag == someInt) { 
        //do something like 
        canvas.backgroundColor = button.backgroundColor; 
        } 
    } 
    
+0

任何示例代碼,讓我開始或我的大腦踢踢會很棒 – madLokesh 2013-05-06 10:57:02

+0

請參閱編輯。 – Amit 2013-05-06 11:07:07

0

你可以把點擊手勢在你的滾動視圖和每當龍頭處於滾動支票是否水龍頭是在ImageView的。 (你會得到是否在圖像視圖中的點擊點),並根據你可以改變圖像。

第二種選擇是採取自定義按鈕並處理其點擊。

希望它可以幫助

+0

任何示例代碼,讓我開始或我的腦海裏踢將真棒 – madLokesh 2013-05-06 10:55:43

+0

我沒有任何樣本代碼,但可以指導你。你正在用第一或第二替代品 – Dhara 2013-05-06 11:00:54

+0

@innocentDemon是否可以用這種方式編碼? – Dhara 2013-05-07 04:54:14

3

實施它很簡單年。

  • 使用UIViewUIImageView
  • UIViewbackgroundColor並將其放置在視圖上滾動
  • 集UITapGestureRecognizer,Refer here
  • 當purticular視圖觸摸你的這 被觸摸的視圖實例
  • 獲取視圖的背景色並將其設置在滾動條中
+0

@LithuTV任何示例代碼,讓我開始或我的心靈踢踢會很棒 – madLokesh 2013-05-06 10:57:44

+0

添加鏈接recogoniser.I認爲它有解決方案爲你的Qn – 2013-05-06 11:08:56

1

而不是按照建議使用UIImageViews或UIViews,我會把自定義的UIButtons用於選擇顏色。

UIButton * colorButton; 

//You will probably do these inside a for loop 
colorButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
[colorButton setFrame:CGRectMake(X, Y, buttonSize, buttonSize)]; 

[colorButton setTitle:@"" forState:UIControlStateNormal]; 

[colorButton setBackgroundColor:SOME_COLOR]; 
//Border visualization would be good if you are putting buttons side-by-side 
[colorButton.layer setBorderWidth:1.0]; 
[colorButton.layer setBorderColor:[[UIColor darkGrayColor] CGColor]]; 

//You can use this tag to identify different buttons later 
[colorButton setTag:INDEX+SOME_OFFSET]; //use loop index with some offset 

[colorButton addTarget:self action:@selector(colorButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 

-(void) colorButtonPressed:(UIButton *) sender 
{ 
    UIColor *selectedColor = sender.backgroundColor; //Or identify with sender.tag 
    [yourCanvas doSmtOnCanvas:selectedColor]; 
} 
1

初始化UIImageView然後通過調用此方法添加手勢識別所有imageViews

- (void) setupImageView : (UIImageView *) imageView{ 
    [imageView setUserInteractionEnabled:YES]; 
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeUIImageViewBackgroundColor:)]; 
    tapGestureRecognizer.numberOfTapsRequired = 1; 
    [imageView addGestureRecognizer:tapGestureRecognizer]; 
} 

現在,通過下列方式改變選擇器內的canvasView顏色:

- (void) changeUIImageViewBackgroundColor : (UITapGestureRecognizer *) recognizer{ 
    UIImageView *imageView = (UIImageView *)[recognizer view]; 
    [canvasView setBackgroundColor:[imageView backgroundColor]]; 
}