Change Brightness of UIScreen - iphone

I am changing UIScreen brightness using value of UISlider i tried this code
self.Slide = [[UISlider alloc] initWithFrame:CGRectMake(13, 0, 244, 23)];
[self.Slide addTarget:self action:#selector(sliderHandler:) forControlEvents:UIControlEventValueChanged];
self.Slide.minimumValue = 0.0f;
self.Slide.maximumValue = 1.0f;
[self.Slide setValue:0.0f];
self.Slide.value = [[UIScreen mainScreen] brightness];
[self.view addSubview:self.Slide];
and method called is
- (void)sliderHandler:(UISlider *)sender {
[[UIScreen mainScreen] setBrightness:sender.value];
}
the method is call but brightness is not changing ......
and how can i set UISlider value to 0 initial ?
thanks....

Please use this code and as for the slider value set maximum and minimum value as given in the code below UISlider Brightness
Whereas the below code would work too.
- (void)viewDidLoad
{
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0,0,300,40)];
[slider addTarget:self action:#selector(sliderHandler:) forControlEvents:UIControlEventValueChanged];
slider.minimumValue = 0.f;
slider.maximumValue = 1.0f;
slider.value = [[UIScreen mainScreen] brightness];
[[self view]addSubview:slider];
[super viewDidLoad];
}
- (void)sliderHandler:(UISlider *)sender
{
//NSLog(#"value:%f", sender.value);
[[UIScreen mainScreen] setBrightness:sender.value];
}
EDIT :
You could try this way too. Add a UIVIew of clear color to the view and increase the alpha component of this view upon the slider value changed event. By doing this , the view gets darkened and we get a feeling that the brightness of the app is reduced. This solution may not be very appropriate, but works just fine.

Related

What piece am I missing to set an iOS background?

I have:
[A series of if statements sets backgroundImage to be something like [UIImageNamed #"Background-Default-Landscape"]; am I right to assume that the device will look for the file named Background-Default-Landscape#2x.png if it is a Retina display?]
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
[containerView addSubview:backgroundView];
Right now my app is launching and once it loads displays a white screen, not a background specified (none of the backgrounds are solid white).
What am I missing to draw a background before I start putting things on the background, further below in the code?
Thanks,
--EDIT--
As far as code, I have:
- (void) renderScreen
{
int height = [[UIScreen mainScreen] bounds].size.height;
int width = [[UIScreen mainScreen] bounds].size.width;
UIImage *backgroundImage = nil;
if (height == 2048 || height == 2008 || height == 1024 || height == 1004 || height == 984)
{
backgroundImage = [UIImage imageNamed:#"Background-Default-Portrait"];
}
// Other such statements cut.
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:backgroundImage];
CGRect containerRect = CGRectZero;
containerRect.size = [backgroundImage size];
UIView *containerView = [[UIView alloc] initWithFrame:containerRect];
[containerView addSubview:backgroundView];
[self.view addSubview:containerView];
Then below that are statements to draw on the background.
This method is called when the initial view loads and when it is rotated:
- (void)viewDidLoad
{
[super viewDidLoad];
[self renderScreen];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(renderScreen) name:UIDeviceOrientationDidChangeNotification object:nil];
}
The behavior is that the correct initial screen loads, then it fades to white and nothing interesting seems to happen after that.
--EDIT--
At the top, I have:
int height = [[UIScreen mainScreen] bounds].size.height;
int width = [[UIScreen mainScreen] bounds].size.width;
When I NSLog the height and width, for a retina device in landscape mode, I get a height of 1024 and a width of 768. The image displayed is a rotation of the portrait image; if I turn the device on the side the image neatly fills the whole screen but as-is the background displays a horizontally squeezed image.
Should I do anything different to be correctly obtaining height and width? I would expect for a device in landscape orientation the height would be 768 and the width would be 1024.
Every time renderScreen fires it will add two more subviews to the current controller's main view:
[containerView addSubview:backgroundView];
[self.view addSubview:containerView];
And this fires every time you rotate the device.
At least have the containerView as a property and replace it.
#property (nonatomic, strong) UIView *containerView;
and inside - (void) renderScreen
[self.containerView removeFromSuperview];
self.containerView = [[UIView alloc] initWithFrame:containerRect];
[self.view addSubview:self.containerView];
You could also add:
[self.view setNeedsDisplay];
So that the view is redrawn.

Hide programmatically created UISlider

I've programmatically created a UISlider in viewDidLoad using the following code and when a button is pressed I want to hide the object and maybe use it again. I can't seem to get it to work. I've tried a number of approaches which all build correctly but none of which have the desired effect.
CGRect frame1 = CGRectMake(-5.0, 290.0, 100.0, 10.0);
UISlider *sliderSaveurFloral = [[UISlider alloc] initWithFrame:frame1];
[sliderSaveurFloral addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[sliderSaveurFloral setBackgroundColor:[UIColor clearColor]];
sliderSaveurFloral.minimumValue = 0.0;
sliderSaveurFloral.maximumValue = 50.0;
sliderSaveurFloral.continuous = YES;
sliderSaveurFloral.value = 0.0;
[self.view addSubview:sliderSaveurFloral];
CGAffineTransform trans2 = CGAffineTransformMakeRotation(M_PI * -0.5);
sliderSaveurFloral.transform = trans2;
[sliderSaveurFloral setValue:0];
UISlider inherits from UIView. I believe you can set the hidden property to make it invisible.
sliderSaveurFloral.hidden = YES; //Set it back to NO when you want it appear again
You may also have to make it not interactive when hidden,
sliderSaveurFloral.userInteractionEnabled = NO; //Set it back to YES later when you need

Postioning UIImageSubview at center in UIScrollview

How to position the UIImageSubview at center in UIScrollview , I am Loading images dynalically in the uiscrollview so when i select any image in the Uiscrollview that Image should display at the center of screen in uiscrollview.
My Image Width is 65 and Screen width is 320 , I am facing problem setting the position of choosen image at the center of screen,
[scrollView1 setContentOffset:CGPointMake(130, 0) animated:YES];
scrollview just scroll to the 130 position. I don't want like this , I want the subview should be displayed at the center.
Please see the screenshot and tell me how to solve this problem.
How about
(pseudocode)
contentOffset.x = image.center.x - (screenwidth/2)
(expanded...)
UIImageView* imageView = //imageview that was selected
CGFloat screenWidth = 320;
CGPoint offset = scrollView1.contentOffset;
offset.x = imageView.center.x - screenWidth/2;
[scrollView1 setContentOffset:offset animated:YES];
I assume you already worked out how to get a pointer to the selected image...
(expanded further...)
Here is a fully-worked out version of the same, using buttons instead of images for ease of selection... try it (just copy into a new single-view project's viewContoller]), you will see that it works. If you are still having problems I expect it is the way you are identifying your image as the selected image for centering. You would need to update your question showing your code...
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect frame = self.view.bounds;
frame.size.height = frame.size.height/2;
self.scrollView = [[UIScrollView alloc] initWithFrame:frame];
[self.view addSubview:self.scrollView];
[self.scrollView setContentSize:CGSizeMake(2000,self.scrollView.bounds.size.height)];
[self.scrollView setDelegate:self];
for (int i = 1; i < 20; i++) {
UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
NSString* title = [NSString stringWithFormat:#"button %d",i];
[button setTitle:title forState:UIControlStateNormal];
button.bounds = CGRectMake(0,0,80,60);
button.center = CGPointMake(i*100+50,self.scrollView.bounds.size.height/2);
[button addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:button];
}
}
- (void)buttonPressed:(UIButton*)sender
{
CGFloat x = sender.center.x - self.scrollView.bounds.size.width/2.0f;
[self.scrollView setContentOffset:CGPointMake(x,0) animated:YES];
}

iOS: Adding UILabel to zoomed UIScrollview was blurred

I am facing one strange issue when adding UILabel to zoomed UIScrollview problem.
I am adding two views to zoomed UIScrollview
1. UIImageView
2. UILabelView
Here the code am using to add UIImageView on UIScrollview
float recentZoomScaleValue = 4.5;
CGRect rect = CGRectMake(x, y, 150, 150);
UIImageView *signatureImage = [[UIImageView alloc]initWithFrame:rect];
[signatureImage setImage:image];
[signatureImage setFrame:rect];
//resize the frame to avoid the auto zoom
CGRect frame = signatureImage.frame;
frame.size.width /= recentZoomScaleValue;
frame.size.height /= recentZoomScaleValue;
[signatureImage setFrame:frame];
[self addSubView:signatureImage];
[self.scrollview addSubView:signatureImage];
Here the code am using to add UILabel on UIScrollview
float recentZoomScaleValue = 4.5;
txtLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 600, self.fontSize)];
[txtLabel setText:#"loganathan is a good boy"];
[txtLabel setFont:[UIFont fontWithName:txtLabel.font.fontName size:self.fontSize]];
[txtLabel setTextColor:[UIColor redColor]];
[txtLabel setBackgroundColor:[UIColor clearColor]];
[txtLabel setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
CGRect frame = signatureImage.frame;
frame.size.width /= recentZoomScaleValue;
frame.size.height /= recentZoomScaleValue;
[txtLabel setFrame:frame];
[self addSubview:txtLabel];
But the problem is when ever i tried to add UILabel view it added with auto zoomed. Since the label text was blurred. I do not know why and what is the problem. Shall i use CATextLayer instead of UILabel?. Any help that might be appreciated.
Thanks
you have to set the contentsScale to the right value when ever the zoomScale changes. Or initial to the current zoomScale:
Example for dynamic zooming:
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
[yourLabel.layer setContentsScale:scale*[[UIScreen mainScreen] scale]];
[yourLabel.layer setNeedsDisplay];
}

UIButtons don't respond to touch after set frame on autorotate

Hello in my app i have some uiButtons added as subviews to a view like in the picture below.
alt text http://img99.imageshack.us/img99/5244/portraitc.png
when the user rotates the phone in landscape the views and buttons must change position to this:
alt text http://img193.imageshack.us/img193/5931/landscapeq.png
initially when the view is in portrait mode the buttons respond to touches. If i tilt the phone the buttons move, everything looks ok like in the second image and the buttons respond to touches. If i tilt the phone back to portrait mode, the buttons move back they look ok but they don't respond to touches. If i change back to landscape the buttons work...
my buttons are created like this:
nextNewsP = [[UIButton alloc] initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width - 40, 10, 25, 25)];
[nextNewsP setImage:[UIImage newImageFromResource:#"next_arrow.png"] forState:UIControlStateNormal];
[nextNewsP addTarget:self action:#selector(nextNewsAction:) forControlEvents:UIControlEventTouchUpInside];
[nextNewsP setShowsTouchWhenHighlighted:TRUE];
[bottomTools addSubview:nextNewsP];
[nextNewsP release];
previousNewsP = [[UIButton alloc] initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width - 85, 10, 25, 25)];
[previousNewsP setImage:[UIImage newImageFromResource:#"previous_arrow.png"] forState:UIControlStateNormal];
[previousNewsP addTarget:self action:#selector(previousNewsAction:) forControlEvents:UIControlEventTouchUpInside];
[previousNewsP setShowsTouchWhenHighlighted:TRUE];
[bottomTools addSubview:previousNewsP];
[previousNewsP release];
bottomTools is a view and is added also as a subview like this:
[self.view addSubview:bottomTools];
[bottomTools release];
and my shouldAutorotateToInterfaceOrientation function looks like this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[UIView beginAnimations:#"moveViews" context: nil];
if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
NSLog(#"set frames for portrait");
closeView.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width - 30, 2, 23, 25);
newsInfo.frame = CGRectMake(10, 10, [[UIScreen mainScreen] bounds].size.width - 10, 22);
internetActivityIndicator.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width - 55, 5, 18, 18);
industryLabel.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width/2.0 - [industryTitle sizeWithFont:[UIFont systemFontOfSize:14]].width/2, 3, [industryTitle sizeWithFont:[UIFont systemFontOfSize:14]].width, 22);
bottomTools.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-40, [[UIScreen mainScreen] bounds].size.width, 40);
nextNewsP.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width - 40, 10, 25, 25);
previousNewsP.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width - 85, 10, 25, 25);
}
else
{
NSLog(#"set frames for landscape");
closeView.frame = CGRectMake([[UIScreen mainScreen] bounds].size.height - 30, 2, 23, 25);
newsInfo.frame = CGRectMake(10, 10, [[UIScreen mainScreen] bounds].size.height - 10, 22);
internetActivityIndicator.frame = CGRectMake([[UIScreen mainScreen] bounds].size.height - 55, 5, 18, 18);
industryLabel.frame = CGRectMake([[UIScreen mainScreen] bounds].size.height/2.0 - [industryTitle sizeWithFont:[UIFont systemFontOfSize:14]].width/2, 3, [industryTitle sizeWithFont:[UIFont systemFontOfSize:14]].width, 22);
bottomTools.frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.width-40, [[UIScreen mainScreen] bounds].size.height, 40);
nextNewsP.frame = CGRectMake([[UIScreen mainScreen] bounds].size.height - 40, 10, 25, 25);
previousNewsP.frame = CGRectMake([[UIScreen mainScreen] bounds].size.height - 85, 10, 25, 25);
}
[UIView commitAnimations];
return YES;
}
did you ever had a similar problem?
thank you in advance,
Sorin
I am actually still struggling with the same problem (a year later ... need iOS 3.0 compatibility). The rotation part was easy. However, the views and buttons don't seem to process touch and move/drag events. I've verified that the window (UIWindow) is receiving the touch events and at the right locations.
It's as if the last 1/3 of the screen (i.e. 480-320) doesn't propagate events to receivers after a rotation to landscape from portrait.
The example in the link by slf doesn't help since the rotated view controller doesn't have responders.
I just figured it out after some view digging. So, I was rotating the viewController.view and doing a bunch of setNeedsLayout on subviews of viewController.view. However, what I needed to do was [self.navigationController.view setNeedsLayout]. That one call fixed everything for me. For whatever reason, the navigationController.view wasn't passing touch events to the part of the subviews that were previously hidden (as far as the frame/bound is concerned). That actually fixed a number of other issues as well.
I think your problem is in your frame adjustment. My gut tells me it has something to do with the logic being inside shouldAutorotateToInterfaceOrientation because that happens before the rotation is actually done. Try doing your math the way they do it in this article and see if that helps.
I've fixed the problem I was having .. I basically needed to do [self.navigationController.view setNeedsLayout].
The way I understand this (which maybe incorrect is that self.navigationController.view.frame was same as self.view.frame and both were equal to (x=0,y=0,width=320,height=480). I then rotated self.view by M_PI/2 and did a number of frame manipulation on select self.view.subviews to get everything to animate/position/scale correctly.
That worked out okay but the navigation controller was not willing to acknowledge touch events to parts of self.view there were to the right of 320. In essence, if this self.navigationController.view.clipsToBounds were true, it might not even have shown that part of self.view.
Anyway, setting setNeedsLayout on the navigation controller's view resolved the issue. I hope this helps someone else. I suspect that was also the problem SorinA was having with buttons not getting touch events.