views:

1401

answers:

2

Hello.

I'm having problems setting the alpha value of a UIView subclass object. Any suggestions?

I have UIView project whose alpha property I'm manipulating to control brightness. I'm using a UISlider control to set the value of the alpha property in a UIView subclass as follows:

-(id)initWithFrame:(CGRect)rect andParent:(id)theParent {

self = [super initWithFrame: rect];

if (self !=nil) {

 [self setParent:theParent];

 theSlider = [[UISlider alloc] initWithFrame: CGRectMake(35.0,400.0,250,0)];
 theSlider.minimumValue = 0.2;
 theSlider.maximumValue = 1.0;
 theSlider.value = 1.0;
 theSlider.continuous = YES;

 UIImage *maximumValueImage = [UIImage imageNamed:@"BrightSun.png"];
 UIImage *minimumValueImage = [UIImage imageNamed:@"DimSun.png"];

 theSlider.maximumValueImage = maximumValueImage;
 theSlider.minimumValueImage = minimumValueImage;

 theSlider.hidden = NO;
 [theSlider addTarget:parent action: @selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];

 [self addSubview:theSlider];

}

return self;
}

The sliderValueChanged method in the parent object changes the alpha value of another UIView subclass object, OverlayView. The parent object is a subclass of UIViewController, myViewController. The slider lives in a second UIView subclass named SliderView.

This seems to work. The problem comes up when I attempt to toggle the .hidden property of the UISlider. When I change the .hidden property to YES,

theSlider.hidden = YES;

The slider disappears, but the alpha value of the OverlayView object resets to 0.0. The value of the slider does not change. I want the alpha property of the OverlayView as set by
sliderValueChanged method to remain unchanged when the hidden property of the UISlider object changes.

Any suggestions as to what I'm doing wrong? Thanks.

A: 

Well, first start off by adding a breakpoint to see if the sliderValueChanged: method is called when you hide your slider which seems to be the case, with the slider value being reset to 0 when you hide it. (seems to be whats going on, its a guess t hough, cant really tell w/o looking and stepping through the code). If what i mentioned is the case then just have a condition in the method (if(!slider.HIDDEN)

Daniel
Thanks. I've already given that I try. The sliderValueChanged: method works as expected. The slider keeps its value.sliderValueChanged: is very simple. - (void) sliderValueChanged: (id)sender { //min = 0.2 //max = 1.0 UISlider *control = (UISlider *)sender; float theValue = control.value; theValue = -1.0 * (theValue - 1.0) + 0.01; self.overlayView.alpha = theValue; }Something to note: the code works as expected when run in the iPhone simulator. The problem materializes when executed on a device.
Ken
A: 

An update. I made a change to the OverlayView class. OverlayView is a subclass of UIView. I overrode the drawRect:(CGRect)rect method as follows:

-(void)drawRect:(CGRect)rect {
    [super drawRect:rect];
}

Now everything works. I'm clueless as to why this fixes the problem. Is drawRect: not called by subclasses of UIView unless specifically overridden?

Ken