简体   繁体   English

在用户干预下设置UIImageView背景颜色

[英]Set UIImageView background color upon user intervention

I am developing an application in which I have a horizontal scroller. 我正在开发一个具有水平滚动条的应用程序。 The scroller area comprise of different colors [which are nothing but UIImageView of colors]. 滚动区域由不同的颜色组成[仅是颜色的UIImageView ]。

Above the scroller area is a canvas [which is also a UIImageView ]. 滚动区上方是画布[也是UIImageView ]。

Now, what I want is that when I select any color in the scroller area, it should set on the canvas . 现在,我想要的是当我在滚动区域中选择任何颜色时,应将其设置在canvas上

It is pretty much like a color picker but I am already providing solid color options to be selected by the user. 它很像一个拾色器,但我已经提供了纯色选项供用户选择。

How should I go about it ? 我应该怎么做? Any sample Code to get me started or my mind kicking would be awesome ? 有任何示例代码可以帮助我入门或提高思维能力吗?

Thanx a ton

To implement it easily. 易于实施。

  • Use UIView not UIImageView 使用UIView而不是UIImageView
  • Set UIView backgroundColor and place it on the scroll 设置UIView backgroundColor并将其放置在滚动条上
  • Set UITapGestureRecognizer on views, Refer here 在视图上设置UITapGestureRecognizer, 请参阅此处
  • when a purticular view is touched you have its instance of view which is touched 当触碰一个轴状视图时,您会看到它的视图实例
  • get the background color of the view and set it in the scroller 获取视图的背景色并将其设置在滚动条中

Can you take UIButtons instead of UIImageViews inside your scrollView to show colours to be picked. 您能否在滚动视图中使用UIButtons代替UIImageViews来显示要选择的颜色。

This will make the implementation easier. 这将使实施更加容易。 User will select any colour button and you will receive a callback in buttons action selector method. 用户将选择任何颜色的按钮,您将在按钮动作选择器方法中收到回调。 Then based on button tag or any property you can set the colour to your canvas. 然后根据按钮标签或任何属性,可以将颜色设置为画布。

In current implementation it will be tricky as you need to know the exact content offset where the tap is done to make out which UIImageView was pressed. 在当前的实现中,这很棘手,因为您需要知道执行点击以确定按下哪个UIImageView的确切内容偏移量。

coding steps: 编码步骤:

  • In your scroll view add UIButtons instead of UIImageView like: 在滚动视图中,添加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]; 
  • In the btnSelected method put this code: 在btnSelected方法中,输入以下代码:

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

Instead of using UIImageViews or UIViews as suggested, I would put custom UIButtons for picking colors. 与其按照建议的方式使用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];
}

Initialize UIImageView then add gesture recognizer to all imageViews by calling this method. 初始化UIImageView然后通过调用此方法将手势识别器添加到所有imageViews

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

Now change color of canvasView within the selector by following way: 现在,通过以下方法在选择器内更改canvasView颜色:

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

You can put a tap gesture in your scrollview and whenever a tap is made in the scroll check whether the tap is on the imageview. 您可以在滚动视图中放置轻击手势,每当在滚动视图中进行轻击时,都要检查轻击是否在图像视图中。 (You will get whether the tap point in on imageview or not) and according you can change the image. (您将获得是否在imageview上点击了点),并根据您可以更改图像。

Second alternative is to take a custom button and handle on its click. 第二种选择是采用自定义按钮并对其单击进行处理。

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM