How to Learn Cocoa Slider coloring example

Source: Internet
Author: User

CocoaLearning pathSliderThe Color Sorting example is the content to be introduced in this article. This development instance is the same as dragging the slide with the mouse, adjusting the three primary color values, thus changing the Final SynthesisColor. New users can use this exercise for NSTextField, NSSlider, NSColor, and NSObject.

1. Create a Cocoa project SliderColor

You need to drag a Text FieldWidget, And four HorizontalSlider ControlTo the window. Text Field is used for display.Color, HorizontalSliderThey are alpha, red, green, and blue. Set the Text Field attribute to read-only (Enabled), Background to black, and HorizontalSliderThe Minimum value (Minimum) is set to 0, the maximum value (Miximum) is 1, and the Continuous check box is selected (this is to change the value when dragging), the Current alpha value (Current) is 1, the other current values are all 0,

2. Create a Controller (NSObject)

Drag an NSObject named ColorController and create Outlets as follows:

 
 
  1. AlphaSlider
  2. BlueSlider
  3. ColorField
  4. GreenSlider
  5. RedSlider
  6. Create Actions as follows:
  7. SetAlpha
  8. SetBlue
  9. SetGreen
  10. SetRed

BIND (Associate) The created Outlets and Actions controls to the window and save the ColorController to the project.

3. write code in ColorController

You need to add a float type variable in ColorController. h to store the Slider value and a void type updateColor method to update the color. The final code is as follows:

 
 
  1. #import <Cocoa/Cocoa.h> 
  2.    
  3. @interface ColorController : NSObject {  
  4.     IBOutlet NSSlider *alphaSlider;  
  5.     IBOutlet NSSlider *blueSlider;  
  6.     IBOutlet NSTextField *colorField;  
  7.     IBOutlet NSSlider *greenSlider;  
  8.     IBOutlet NSSlider *redSlider;  
  9.    
  10.     float redValue;  
  11.     float blueValue;  
  12.     float greenValue;  
  13.     float alphaValue;  
  14. }  
  15. - (IBAction)setAlpha:(id)sender;  
  16. - (IBAction)setBlue:(id)sender;  
  17. - (IBAction)setGreen:(id)sender;  
  18. - (IBAction)setRed:(id)sender;  
  19. - (void)updateColor;  
  20. @end 

Add the updateColor method to ColorController. m to convert the RGB color and set the background color of the Text Field. Call updateColor by dragging. The complete code is as follows:

 
 
  1. # Import "ColorController. h"
  2. @ Implementation ColorController
  3. -(IBAction) setAlpha :( id) sender {
  4. [Self updateColor]; // call the updateColor Method
  5. }
  6. -(IBAction) setBlue :( id) sender {
  7. [Self updateColor];
  8. }
  9. -(IBAction) setGreen :( id) sender {
  10. [Self updateColor];
  11. }
  12. -(IBAction) setRed :( id) sender {
  13. [Self updateColor];
  14. }
  15. -(Void) updateColor {
  16. // Obtain the RGB and Alpha values and assign them to the variables.
  17. RedValue = [
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.