simplest (rectangular) drop shadow for an UIView - ios

I've seen lots of snippets that either: are too complicated for something as simple as a drop shadow, requiring subclassing UIView and using quartz2d calls, or I can't get them to work.
I just want to do this on a view I'm adding as a subview to another one (the subview is taken from another viewController I'm just allocating - I know that's probably not nice but oh well), no IB or anything. what's the simplest / most accepted way to go about it? would it be different if I want it to work on iOS 4?

It's as easy as importing <QuartzCore/QuartzCore.h> and using a similar snippet as below:
self.viewAboutContainer.layer.shadowColor = [[UIColor blackColor] CGColor];
self.viewAboutContainer.layer.shadowOpacity = 0.7;
self.viewAboutContainer.layer.shadowRadius = 4.0;
self.viewAboutContainer.layer.shadowOffset = CGSizeMake(5.0f, 5.0f);
self.viewAboutContainer.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.viewAboutContainer.bounds].CGPath;

Related

How can I achieve rounded sides effect on UIView? I have attached a sample image

I want to achieve this effect on UIView. I have tried with UIBezierPath but no success so far.
Below is sample image.
Check below answer,
import #import <QuartzCore/QuartzCore.h>
and write code
view.layer.cornerRadius = 5;
view.layer.masksToBounds = true;
Another different approach is
[view.layer setCornerRadius:25.0f];
[view.layer setMasksToBounds:YES];
I'm not sure if this can help but, if you select the item of interest from the storyboard, select the following icon and in the box type layer.cornerRadius and change boolean to string and then type whatever multiplier you want.

Making transparent Circles in iOs

I want to create a black UIView with transparent circles.
I think about create one view (with black color and transparence 50%), and add multiple circles inside of it, but I don't know how to set the transparence for each. I know how to create a circle View (an example: how to draw a custom uiview that is just a circle iphone-app).
I want to do is something like iShowcase library but with multiple dots:
Any clue? thanks.
SOLVED
I took a look to the code of iShowcase library and I solved my probblem. now, I am working in a library based in iShowcase library.
I will post here when I finish it.
Please have a look of below link hope this will helpful for you.
Link : Here is Answer to set shadow in your view.
Use alpha for your circleView. As in your link example,then add as subviews in yourmainview:
UIView *circleView = [[UIView alloc] initWithFrame:CGRectMake(10,20,100,100)];
circleView.alpha = 0.5;
circleView.layer.cornerRadius = 50;
circleView.backgroundColor = [UIColor whiteColor];
[yourmainview addSubview: circleView];
Btw in your picture I think white circles have 100% alpha. You can use individual alpha for each circleView, or use a randomizer :)
As for updated example why don't you add more buttons and showcase in your h file, synthesize them and use multiple instances .... showcase setupShowcaseForTarget:btn_custom_1 title:#"title" details:#"other"]; ? I think you should modify main classes, becouse what you want are different containerView for multiple views [circles].
Using modifyed iShowcase.m [- (void) calculateRegion], and different views as containers, I was able to make something like: http://tinypic.com/view.php?pic=2iwao6&s=8#.VLPTRqYsRE8 So the answer is: use custom views for multiple showcase [ex [showcase2 setContainerView:self.view2];], then custom frame for each showcase [ showcase2.frame = CGRectMake(0,0,100,100);] I don;t habe time to fine tuning the example, but yes, you can achieve desired result...
I finally solved my question inspired by iShowCase library I did this simple class and Upload to github.
https://github.com/tato469/FVEasyShowCase
Simplest what you can do is to have your main view (black 50% transparant) and add shapes to the mask layer of that.
So basically:
//Set up your main view.
UIView* mainView = [UIView new];
mainView.backgroundColor = [UIColor blackColor];
mainView.alpha = 0.5;
UIView* circle1 = [YourCircleClassHere new];
UIView* circle2 = [YourCircleClassHere new];
UIView* circle3 = [YourCircleClassHere new];
UIView* container = [UIView new];
[UIView addSubview:circle1];
[UIView addSubview:circle2];
[UIView addSubview:circle3];
//Make a new layer to put images in to mask out
CALayer* maskLayer = [CALAyer layer];
//Assign the mask view to the contents layer.
maskLayer.contents = (id)container;
//This will set the mask layer to the top left corner.
maskLayer.frame = CGRectMake(0,0,container.frame.size.width,container.frame.size.height);
//Lastly you assign the layer to the mask layer of the main view.
mainView.layer.mask = maskLayer;
//Applies basically the same as clipToBounds, but a bit reversed..
mainView.layer.mask = true/false;
On a sidenote:
I achieved this with images "contents = (id) [UIImage CGImage]", but I'm sure it should work with UIViews as well.
Also mind some mistakes, since I just wrote this from my mind, also I didn't test this out.. So keep me updated if it works/!works ^_^

Add style for every buttons in ViewController

i have over 20 buttons in my XCode project, and i want to add border to every of them. is it possible to like select all the UIButtons in the storyboard and then perform codes on them?
// For example
buttons.borderColor = [[UIColor darkGrayColor] CGColor];
buttons.cornerRadius = 8;
buttons.borderWidth = 1;
You could use the UIAppearance protocol.
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAppearance_Protocol/index.html

How should I use UIButtons with the iOS 6 style in iOS 7?

I'm making an app designed for iOS 6 and 7, and I want to allow users to choose which style they want: iOS 6 or iOS 7. Or at least just make it have the iOS 6 style completely. The app needs to be acceptable on the App Store.
I'm considering trying to find iOS 6 button images to use, but since I'm new to iOS GUI programming, I'm wondering if there is a cleaner way to do this that I don't know about. Is there a framework I could simply add so that I can use something like "[[UIButton6 alloc] init]" etc to make buttons with the older style? Or could I easily do this myself with rendering settings?
Any other approaches would also be helpful. Also, answers saying that this isn't possible at all are fine, but please no answers telling me that I should just use the iOS 7 GUI. I'm asking "how/can" I do this, not "should" I do this.
What I do is to add the drawing of an oval shape with a gradient background, myself. For example, here's code from one of my apps:
CAGradientLayer* grad = [CAGradientLayer new];
grad.frame = CGRectMake(0,0,15,15);
grad.colors = #[(id)[UIColor colorWithRed:1 green:1 blue:0 alpha:0.8].CGColor,
(id)[UIColor colorWithRed:.7 green:.7 blue:.3 alpha:0.8].CGColor];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(15,15), NO, 0);
UIBezierPath* p = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0,0,15,15) cornerRadius:8];
[p addClip];
[grad renderInContext:UIGraphicsGetCurrentContext()];
[[UIColor blackColor] setStroke];
p.lineWidth = 2;
[p stroke];
UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
im = [im resizableImageWithCapInsets:UIEdgeInsetsMake(7,7,7,7) resizingMode:UIImageResizingModeStretch];
Now use im as the background image of the button.
That code is just a "serving suggestion"; try it and then change the gradient colors or get rid of the gradient or whatever you feel like doing.
RED FLAG:
If you want to be accepted on the App Store, then intentionally targeting the iOS 6 look and feel is a serious danger.
As of February 1st, Apple is expecting all applications to be "optimized for iOS 7", which doesn't immediately exclude what you're discussing, but I wouldn't risk it personally.
Matt's answer is good, but I found a way to do this without rendering an image. Might be faster, and the code is more compact. I also like it a lot better when the gradient has 3 stops:
CAGradientLayer* grad = [CAGradientLayer new];
grad.frame = CGRectMake(0,0,button.frame.size.width,button.frame.size.height);
grad.colors = #[(id)[UIColor colorWithWhite:1 alpha:1].CGColor,(id)[UIColor colorWithWhite:.95 alpha:1].CGColor,(id)[UIColor colorWithWhite:1 alpha:1].CGColor];
grad.locations = #[#(0), #(.5), #(1)];
grad.cornerRadius = 10;
grad.borderColor = [UIColor blackColor].CGColor;
grad.borderWidth = .2;
[button.layer insertSublayer: grad atIndex:0]; //button is the UIButton you want to modify
Of course, edit the gradients, border, etc as needed. Here's what it looks like:
P.S. In case anyone "ninja'd" me, I originally said in this post that matt's answer doesn't allow you to set the gradient stops, but I was wrong.

How to make UIView with AVCaptureVideoPreviewLayer look better?

I'm trying to build a subclass of UIView to quickly take one snapshot by using AVFoundation.
It's working great but I would really like to make the corners round or add a little shadow.
How can I achieve this using Core Graphics etc.?
GitHub: https://github.com/dariolass/QuickShotView
Update: This is what the result looks like:
http://www.bytolution.com/qsv.png
Use QuartzCore
#import <QuartzCore/QuartzCore.h>
then you can modify the layer off your the view:
someView.layer.cornerRadius = 8;
someView.layer.shadowOffset = CGSizeMake(-15, 20);
someView.layer.shadowRadius = 5;
someView.layer.shadowOpacity = 0.5;
or something like that

Resources