multiple shadowOffset on UIViewController - ios

I've looked on the web but my results mainly consist of "how to do a shadowOffset." I need to apply "Shadow,Border, and CornerRadius" to multiple objects in my views. I just wanted to see if theres a more effienct way? Or if theres a way to keep it more organized. heres some of my code. Keep in mind I have multiple views like this, so the amount of space this code takes gets pretty annoying.
topView.layer.cornerRadius = 3;
topView.layer.masksToBounds = YES;
topView.layer.borderColor= [UIColor lightGrayColor].CGColor;
topView.layer.borderWidth = 0.5f;
bottomView.layer.cornerRadius = 3;
bottomView.layer.masksToBounds = YES;
bottomView.layer.borderColor= [UIColor lightGrayColor].CGColor;
bottomView.layer.borderWidth = 0.5f;
eventName.layer.masksToBounds = NO;
eventName.layer.shadowColor = [UIColor blackColor].CGColor;
eventName.layer.shadowOpacity = 0.5;
eventName.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
eventName.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
addressLabel.layer.masksToBounds = NO;
addressLabel.layer.shadowColor = [UIColor blackColor].CGColor;
addressLabel.layer.shadowOpacity = 0.5;
addressLabel.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
addressLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
dateLabel.layer.masksToBounds = NO;
dateLabel.layer.shadowColor = [UIColor blackColor].CGColor;
dateLabel.layer.shadowOpacity = 0.5;
dateLabel.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
dateLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
typeLabel.layer.masksToBounds = NO;
typeLabel.layer.shadowColor = [UIColor blackColor].CGColor;
typeLabel.layer.shadowOpacity = 0.5;
typeLabel.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
typeLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
eventCaption.layer.masksToBounds = NO;
eventCaption.layer.shadowColor = [UIColor blackColor].CGColor;
eventCaption.layer.shadowOpacity = 0.5;
eventCaption.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
eventCaption.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);

If most of your shadows are identical, you could choose to loop over an array (or a set, since you don't really care about the order of the items it is applied to) of your views to apply the same shadow ?
That is, I see 2 kind of shadows in the code you pasted here, the lightGray and the black.
You could do something like :
NSArray * blackShadowItems = #[eventName, addressLabel, dateLabel];
for (UIView * view in blackShadowItems) {
view.layer.masksToBounds = NO;
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOpacity = 0.5;
view.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
view.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
}
or declare another function :
- (void)setBlackShadow:(UIView *)view {
view.layer.masksToBounds = NO;
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOpacity = 0.5;
view.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
view.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
}
You can combine both of these solutions.
Finally, note that if these views are declared in a .xib file, you could declare an IBOutletCollection to regroup the views according to the type of shadow you want to set on it.
This is some quite similar to declaring your NSArray or NSSet yourself.

Related

Can SKEmitterNode particles be rotated around their X and Y axes? [duplicate]

I'm trying to reproduce pieces of small paper falling from top effect, using CAEmitterLayer & CAEmitterCell.
So far, I got the 2D animation of it, But I'm having difficulty to make each cell to rotate when falling.
How to I apply random rotation on each particle? I tried with 3D Transform with no success so far.
This is what I got:
-(void) configureEmitterLayer {
self.emitterLayer = [CAEmitterLayer layer];
self.emitterLayer.emitterPosition = CGPointMake(self.backgroundContainer.bounds.size.width /2, 0);
self.emitterLayer.emitterZPosition = 10;
self.emitterLayer.emitterSize = self.backgroundContainer.bounds.size;
self.emitterLayer.emitterShape = kCAEmitterLayerLine;
CAEmitterCell *emitterCell = [CAEmitterCell emitterCell];
emitterCell.contents = (__bridge id)([UIImage imageWithColor:[UIColor whiteColor]].CGImage);
emitterCell.lifetime = CGFLOAT_MAX;
emitterCell.lifetimeRange = 4.0;
emitterCell.birthRate = 2.5;
emitterCell.color = [[UIColor colorWithRed:1.0f green:1.0 blue:1.0 alpha:1.0] CGColor];
emitterCell.redRange = 1.0;
emitterCell.blueRange = 1.0;
emitterCell.greenRange = 1.0;
emitterCell.alphaRange = 0.3;
emitterCell.velocity = 10;
emitterCell.velocityRange = 3;
emitterCell.emissionRange = (CGFloat) M_PI_2;
emitterCell.emissionLongitude = (CGFloat) M_PI;
emitterCell.yAcceleration = 1;
emitterCell.zAcceleration = 4;
emitterCell.spinRange = 2.0;
emitterCell.scale = 7.0;
emitterCell.scaleRange = 4.0;
self.emitterLayer.emitterCells = [NSArray arrayWithObject:emitterCell];
[self.backgroundContainer.layer addSublayer:self.emitterLayer];
}
This library doesn't use the emitter framework but is worth looking at for inspiration. UIDynamics is attached to individual objects to achieve the desired effect.
iOS confetti example

Why CGColorRef does not change the CALayer properties?

I wanted to change view properties using CALayer associated with it.
I have created one function:
-(void)setupViewLayer{
viewLayer1.backgroundColor = (__bridge CGColorRef _Nullable)([UIColor blueColor]);
viewLayer1.borderColor =(__bridge CGColorRef _Nullable)([UIColor redColor]);
viewLayer1.borderWidth = 100.0;
viewLayer1.shadowOpacity = 0.7;
viewLayer1.shadowRadius = 10.0;
}
viewLayer1 is view's layer whose properties I want to change.
But the view does not show the properties.
Is there anything else I need to change? Please help.
Use this code -
viewLayer1.backgroundColor = [UIColor blueColor].CGColor;
viewLayer1.borderColor = [UIColor redColor].CGColor;
viewLayer1.borderWidth = 100.0;
viewLayer1.shadowOpacity = 0.7;
viewLayer1.shadowRadius = 10.0;
Hope this helps!
Set Color by,
viewLayer1.backgroundColor = [UIColor blueColor].CGColor;
viewLayer1.borderColor = [UIColor redColor].CGColor;

How do I change UITextField border from itself?

I have MYTextField inherited from UITextField.
I call [self changeBorderColor] method inside MYTextField that looks like that:
self.layer.masksToBounds = YES;
self.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor brownColor]);
self.layer.borderWidth = 1;
But nothing happen. Is there way to do that?
instead of self.layer.borderColor = (__bridge CGColorRef _Nullable)([UIColor brownColor]);
write self.layer.borderColor = [UIColor brownColor].CGColor;
By using the layer property you can do like this way,
tf.layer.cornerRadius = 25.0f;
tf.layer.masksToBounds = YES;
tf.layer.borderColor = [UIColor brownColor].CGColor;
tf.layer.borderWidth = 1.5f;

CAEmitterCell - slow stars or light effect

I'm playing around with CAEmitterCell, but most effects are very fast effects like the firework.
But I want to have a slow "stars" effect like you see here on www.findyourwaytooz.com
How can I do that with CAEmitterCell?
Thank you :)
I have a project that uses the following setup for the emitter, and it pretty accurately mimics what I think you mean:
//set ref to the layer
starsEmitter = (CAEmitterLayer*)self.layer; //2
//configure the emitter layer
starsEmitter.emitterPosition = CGPointMake(160, 240);
starsEmitter.emitterSize = CGSizeMake(self.superview.bounds.size.width,self.superview.bounds.size.height);
NSLog(#"width = %f, height = %f", starsEmitter.emitterSize.width, starsEmitter.emitterSize.height);
starsEmitter.renderMode = kCAEmitterLayerPoints;
starsEmitter.emitterShape = kCAEmitterLayerRectangle;
starsEmitter.emitterMode = kCAEmitterLayerUnordered;
CAEmitterCell* stars = [CAEmitterCell emitterCell];
stars.birthRate = 0;
stars.lifetime = 10;
stars.lifetimeRange = 0.5;
stars.color = [[UIColor colorWithRed:255 green:255 blue:255 alpha:0] CGColor];
stars.contents = (id)[[UIImage imageNamed:#"particle.png"] CGImage];
stars.velocityRange = 500;
stars.emissionRange = 360;
stars.scale = 0.2;
stars.scaleRange = 0.1;
stars.alphaRange = 0.3;
stars.alphaSpeed = 0.5;
[stars setName:#"stars"];
//add the cell to the layer and we're done
starsEmitter.emitterCells = [NSArray arrayWithObject:stars];
I uploaded the sample project to GitHub: SimpleCAEmitterLayer

Add shadow to frame of UITextView add also shadow to text

I have a UITextView where I add some drop shadow to the frame, but when I write, the text got also the same shadow. How to avoid this problem?
My code :
commentary = [[UITextView alloc]initWithFrame:CGRectMake(10, 435, 230, 120)];
commentary.font = STANDARDFONT;
commentary.backgroundColor = BACKGROUND;
commentary.layer.shadowColor = [UIColor blackColor].CGColor;
commentary.layer.shadowOffset = CGSizeMake(2, 2);
commentary.layer.shadowOpacity = 0.8;
commentary.layer.shadowRadius = 2.0;
commentary.layer.borderColor = [UIColor grayColor].CGColor;
commentary.layer.borderWidth = 1.5;
commentary.layer.cornerRadius = 5;
commentary.layer.masksToBounds = NO;
commentary.clipsToBounds = NO;
[self addSubview:commentary];
BACKGROUND and STANDARDFONT is [UICOLOR clearColor].CGColorand [UIFont fontWithName:#"TimesNewRomanPSMT" size:16];
Try setting the layer background color also: commentary.layer.backgroundColor = BACKGROUND.CGColor

Resources