我正在開發一個應用程序,其中有一個水平滾動條。 滾動區域包含不同的顏色[只是顏色的UIImageView
]。用戶干預後設置UIImageView背景色
在滾動區域上方是一個畫布[也是一個UIImageView
]。
現在,我想要的是,當我在滾動區域選擇任何顏色時,它應該設置在畫布。
它非常像一個顏色選擇器,但我已經提供了純色選項供用戶選擇。
我應該怎麼辦?任何示例代碼,讓我開始或我的大腦踢踢會很棒?
感謝名單一噸
我正在開發一個應用程序,其中有一個水平滾動條。 滾動區域包含不同的顏色[只是顏色的UIImageView
]。用戶干預後設置UIImageView背景色
在滾動區域上方是一個畫布[也是一個UIImageView
]。
現在,我想要的是,當我在滾動區域選擇任何顏色時,它應該設置在畫布。
它非常像一個顏色選擇器,但我已經提供了純色選項供用戶選擇。
我應該怎麼辦?任何示例代碼,讓我開始或我的大腦踢踢會很棒?
感謝名單一噸
你可以把你滾動視圖內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;
}
}
實施它很簡單年。
UIView
不UIImageView
UIView
backgroundColor
並將其放置在視圖上滾動@LithuTV任何示例代碼,讓我開始或我的心靈踢踢會很棒 – madLokesh 2013-05-06 10:57:44
添加鏈接recogoniser.I認爲它有解決方案爲你的Qn – 2013-05-06 11:08:56
而不是按照建議使用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];
}
初始化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]];
}
任何示例代碼,讓我開始或我的大腦踢踢會很棒 – madLokesh 2013-05-06 10:57:02
請參閱編輯。 – Amit 2013-05-06 11:07:07