How to add specific blend mode to custom AccessoryView - objective-c

i have a tableview and i wish to customize accessoryview button.
For that i have custom PNG image with semi-transparent pixels.
UIButton *myAccessoryButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
[myAccessoryButton setUserInteractionEnabled:YES];
[myAccessoryButton setBackgroundColor:[UIColor clearColor]];
[myAccessoryButton setImage:[UIImage imageNamed:#"accessory_btn"] forState:UIControlStateNormal];
[myAccessoryButton addTarget: self
action: #selector(accessoryButtonTapped:withEvent:)
forControlEvents: UIControlEventTouchUpInside];
[cell setAccessoryView:myAccessoryButton];
And that works fine for me button draws, except one trouble. In my template (PSD) this button has blend mode - overlap, and when i export that button from PSD format it have white semi-transparent pixels which must blends with table background in same overlap blend-mode. But by default exported PNG blends in default mode when i load it as UIImage to customize my accessoryview.
How to change blend mode of UIImage or UIButton or AccessoryView may be?

Is the picture on the left what you get in Photoshop? You want it to brighten the background without altering its hue? If so, there's no easy way to achieve that.
There are two partial solutions:
Have a single drawRect block (or its CALayer equivalent) which draws the background and the draws the glow using the appropriate CGBlendMode.
Edit the picture so the glow at the bottom is the shade of brown of your current background.
Either way, you lose flexibility; it would be nice to drag around your buttons or change the background graphic and have everything merge together nicely.
This may change. CALayer already has a property called compositingFilter. However, as of iOS 5.1, its behavior is undefined. If anyone reads this in a future where iOS does have compositing filter that work, please add a comment to say so.

Related

Button above image with zoom objective c

I need to show an image, and above it some buttons that correspond to certain clickable areas of the image, the problem is that the image has zoom, and when i zoom the image, the buttons do not stay in the same place as the picture. The solution was to use the class ImageScrolView, used in the example of Apple PhotoScroller. The button appears exactly in the right place, regardless of zoom.
Just like the example, i use a ScrollView to display multiple images, these images are shown by class ImageScrollView.
This class declares one UIView in the .H file, but in the implementation file, the method that displays the image, convert the UIView into a UIImageView
imageView = [[UIImageView alloc] initWithImage: image];
then create my button and add to the imageView.
amazon = [UIButton buttonWithType: UIButtonTypeRoundedRect];
amazonia.frame = CGRectMake (92,240,109,142);
[amazon addTarget: self action: # selector (selectState :) forControlEvents: UIControlEventTouchUpInside];
[imageView addSubview: amazon];
My problem is that the button does not respond to button events, it appears in the right place and everything, but does not respond to button events.
Any ideas? Ideias on how to implement those clickable areas on the image are welcome too.
make sure to set imageView.userInteractionEnabled = YES!
Hope that helps.

Making rest of view gray when using UISearchBar

I want to mimic what seem to be the standard UI for using the UISearchBar, and right now I trying to make the rest of the view gray, when I begin searching, and I tried doing that by just setting the background color of the view to gray, but I am using sections in my UITableView, and they are not turning gray. Anybody have any reasons why that is?
The reason you're not seeing the section headers fade to gray is because they are drawn on top of the gray background, and are opaque; in other words, their alpha is 1.
If you're looking for a suggestion to get the effect you want, my first reflex would be to add an overlay UIView as a subview of the area you want to be "grayed out", and change its background color when certain events occur. Something like this:
// You have to choose what view is the one that needs to be grayed out.
// I'll call the view you want to gray out "viewToBeGrayed"
UIView *grayOverlay = [[UIView alloc] initWithFrame:viewToBeGrayed.frame];
// Initially, the overlay should be clear
[grayOverlay setBackgroundColor:[UIColor clearColor]];
[viewToBeGrayed addSubview:grayOverlay];
Then, when you want to "gray out" viewToBeGrayed, just change the background color of grayOverlay to some translucent gray value:
[grayOverlay setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.5]];
Finally, when you want to get rid of the "grayed out" effect, just set the view back to clear:
[grayOverlay setBackgroundColor:[UIColor clearColor]];
That should be a good place to start. Comment if you need any additional help.
Use the standard UISearchController for your tableview search needs.

Add background shadow to UIButton

How can add a light gray shadow to a UIButton, I don't want a method to do this at the moment, it should be something like:
UIButton *button1... button1.layer.shadowOpacity = 0.8
etc, but that doesn't work, it only adds a shadow inside the button, but I need it on the outside. Thanks!
First you have to #import <QuartzCore/QuartzCore.h>. Then:
mybtn.layer.shadowColor = [UIColor blackColor].CGColor;
mybtn.layer.shadowOpacity = 0.5;
mybtn.layer.shadowRadius = 2;
mybtn.layer.shadowOffset = CGSizeMake(3.0f,3.0f);
You can also use –[UIButton setBackgroundImage:forState:] to set the background image for UIControlStateNormal to one with a shadow. E.g.:
[button setBackgroundImage:[UIImage imageNamed:#"ButtonBackgroundNormal"]
forState:UIControlStateNormal];
where ButtonBackgroundNormal.png has a shadow. Images often render faster than drawing with code. And, speed is important, especially if you're adding it to a UITableViewCell. In that case, to speed up scrolling speed, make sure the background image is completely opaque by designing it with the same background color of the UITableViewCell and saving it without transparency. Then, set button.opaque = YES.

Draw Over Image

I'm working on some drawing code. I have that portion working great.
I want to draw over an image, but I want to still be able to see the detail of the image, the black lines, etc.
What I am working on is making a transparent UIImageView that holds the image.
I'm not sure how to get this set up properly though.
Should this be added above the other UIImageView that I color on or below it?
Here's what I have so far:
- (void)viewDidLoad
{
[super viewDidLoad];
topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 46, 320, 370)];
[topImageView setImage:[UIImage imageNamed:#"imagesmall.png"]];
topImageView.alpha = 1.0;
topImageView.layer.opacity = 1.0;
topImageView.layer.opaque = NO;
[self.view addSubview:topImageView];
[topImageView release];
}
Thoughts anyone?
Yes, you can draw views over other views. They are drawn in the order that they're added as subviews, unless you reorder them after that.
You may need to set the opaque property for some views (this is distinct from and overrides their layer opacity), and set their backgroundColor to nil. UIImageView seems to be transparent by default, as long as its image is; some other UIView subclasses are not.
So, just what is your overlay going to be? If you just need to display one image over another, what you have here seems to work already. If you need to draw some lines programmatically, you'll need to do this:
Create a subclass of UIView.
Implement its drawRect method to display the content you need.
When you add your custom view on top of the background image, make sure it is not opaque and has no backgroundColor.
A common problem here is to find that your foreground is working, but the background isn't being loaded properly. To make sure the background is there, set the alpha of the foreground view to 0.5. You won't want to do that in production, but it will allow you to verify that both views exist.

How to handle autoresizing on UIButton elements which has been created dynamically

I create some buttons dynamically on my view and i decide their frames according to some of my JSON respone parameters. But i want to autoresize them when the device(dsimulator) rotates. I can easily do this on interface builder but can't do anything on dynamic ones. Can someone help?
EDIT
Here is a snipped of my code
if (button.tag==1) {
button.frame = CGRectMake(30.0f, yPosition, 200.0f, buttonHeight);
}
if (button.tag==2) {
button.frame = CGRectMake(280.0f, yPosition, 200.0f, buttonHeight);
}
if (button.tag==3) {
button.frame = CGRectMake(530.0f, yPosition, 200.0f, buttonHeight);
}
There is no problem when using Portraid mode but when it rotates to Landscape a big empty area stays on the right side of the screen. How can i fix this?(I mean, when i rotate, i want the buttons got to the center of the scren's width)
EDIT:
I played with autoresizing on Size Inspector(Xcode 4.3) with my xib file and it works great, but whatever i did i couldn't resize the dynamically created buttons after rotation. I tried almost all of AutoresizingMask enums of UIView but nothing changes. Can someone please help
You can define by code what was the expected behavior when the device is rotated.
You can take a look at: http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html
and
http://developer.apple.com/library/ios/#documentation/uikit/reference/uiview_class/uiview/uiview.html#//apple_ref/c/tdef/UIViewAutoresizing
You need to set the button behavior when you add it like:
[button setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin];
The attributes you've set up in your interface builder file are the UIViewAutoresizing attributes.
Take a look at this documentation from Apple on the UIView class (which your button is a subclass of); look for the UIViewAutoresizing attribute. That's the one you'll want.
Update: Here's a snippet of code for an MKMapView that uses this ability:
mainMapView = [[MKMapView alloc] initWithFrame:CGRectMake(20, 239, 280, 122)];
[mainMapView setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
With UIViewAutoresizingFlexibleTopMargin, the map view moves down when the user answers the phone --- it fixes the map's position relative to the top of the screen.
Dig around in the documentation and you'll find the autoresizing mask that works best for your situation.