我正在尝试创建一个具有三个UISlider的应用程序,用于选择R,G和B值。滑块应根据滑块值更改标签的颜色。
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSInteger color;
IBOutlet UISlider *redSlider;
IBOutlet UISlider *greenSlider;
IBOutlet UISlider *blueSlider;
IBOutlet UILabel *colorLabel;
}
@property (nonatomic, retain) UISlider *redSlider;
@property (nonatomic, retain) UISlider *greenSlider;
@property (nonatomic, retain) UISlider *blueSlider;
colorLabel.textColor = [UIColor colorWithRed: redSlider.value green:greenSlider.value
blue:blueSlider.value alpha:1];
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@synthesize redSlider, greenSlider, blueSlider;
@end
这是我到目前为止所做的事情,而且我完全陷入困境。
答案 0 :(得分:3)
你可以这样做......
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
// you need to make sure these are actually created
// and give them all a target of self and action sliderValueChanged
@property (nonatomic, retain) UISlider *redSlider;
@property (nonatomic, retain) UISlider *greenSlider;
@property (nonatomic, retain) UISlider *blueSlider;
// not sure where this is set up
@property (nonatomic, strong) UILabel *colorLabel;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// You may have to create your labels and sliders in here...
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)sliderValueChanged
{
// the sliders have changed so set the colour of the label
// based on the new values of the sliders.
self.colorLabel.textColor = [UIColor colorWithRed:self.redSlider.value
green:self.greenSlider.value
blue:self.blueSlider.value
alpha:1.0];
}
@end
答案 1 :(得分:1)
因为你实际上没有问题我会猜测你的代码没有编译,因为它不会。
colorLabel.textColor = [UIColor colorWithRed: redSlider.value green:greenSlider.value blue:blueSlider.value alpha:1];
以上行不属于您的界面。我建议你将目前的代码更改为。
<强> ViewController.h 强>
@interface ViewController : UIViewController
// You don't need the ivars anymore these get generated with the properties below
@property (nonatomic, retain) IBOutlet UISlider *redSlider;
@property (nonatomic, retain) IBOutlet UISlider *greenSlider;
@property (nonatomic, retain) IBOutlet UISlider *blueSlider;
@property (nonatomic, retain) IBOutlet UILabel *colorLabel
@property (assign) NSInteger color;
@end
<强> ViewController.m 强>
@implementation ViewController
// Also no need for the synthesize as these also get generated automatically
- (void)viewDidLoad
{
[super viewDidLoad];
// To change the actual color when changing the value of a slider move this to a method that gets called on slider changed.
colorLabel.textColor = [UIColor colorWithRed:self.redSlider.value green:self.greenSlider.value self.blue:blueSlider.value alpha:1];
}
@end
一旦你真正问了一个问题,我也可能会帮助你。
答案 2 :(得分:0)
您的ViewController.h: -
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
IBOutlet UISlider *redSlider;
IBOutlet UISlider *greenSlider;
IBOutlet UISlider *blueSlider;
IBOutlet UILabel *colorLabel;
}
@property (nonatomic, retain) UISlider *redSlider;
@property (nonatomic, retain) UISlider *greenSlider;
@property (nonatomic, retain) UISlider *blueSlider;
@end
并且你的ViewController.m将是这样的: -
#import“ViewController.h”
@interface ViewController ()
@end
@implementation ViewController
@synthesize redSlider, greenSlider, blueSlider;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self changeLabelColor];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(Void) changeLabelColor
{
colorLabel.backgroundcolor = [UIColor colorWithRed:redSlider.value green:redSlider.value blue:redSlider.value alpha:1.0];
}
@end
由于我没有复制粘贴并且我已经写过,可能会有一些错字。 textcolor不会改变标签颜色,所以我把它改成了backgroundcolor。 你也可以用IBAction添加滑块方法。