Layer zPosition has no effect - ios

Three layers are given the same parent, but the zPosition value on the first layer to be added has no effect - it remains at the back. Why is this? The book I'm reading says that the position in the sublayers array can be overridden with the zPosition value.
import UIKit
import PlaygroundSupport
extension CGRect {
init(_ x:CGFloat, _ y:CGFloat, _ w:CGFloat, _ h:CGFloat) {
self.init(x:x, y:y, width:w, height:h)
}
}
let v0 = UIView(frame: CGRect(0, 0, 500, 500))
v0.backgroundColor = .white
let v1 = UIView(frame: v0.frame)
let lay1 = CALayer()
lay1.backgroundColor = UIColor(red: 1, green: 0.4, blue: 1, alpha: 1).cgColor
lay1.frame = CGRect(113, 111, 132, 194)
lay1.zPosition = 100
v1.layer.addSublayer(lay1)
let lay2 = CALayer()
lay2.backgroundColor = UIColor(red: 0.5, green: 1, blue: 0, alpha: 1).cgColor
lay2.frame = CGRect(41, 56, 132, 194)
v1.layer.addSublayer(lay2)
let lay3 = CALayer()
lay3.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 1).cgColor
lay3.frame = CGRect(43, 197, 160, 230)
v1.layer.addSublayer(lay3)
v0.addSubview(v1)
PlaygroundPage.current.liveView = v0

I think your code is working as expected. I just changed the colors of the view to
lay1.backgroundColor = UIColor.green.cgColor
lay2.backgroundColor = UIColor.blue.cgColor
lay3.backgroundColor = UIColor.red.cgColor
This is the output. You can see green view(lay1) which has z position is at the top.
Also , had fun playing with literals here:

Code works as expected in an app. Seems that it's an issue with Playground.

Related

Applying gradient to UIImage

I want to apply color gradient(from top to bottom) to an image named 'newspaperImage'. This is my code using swift language.
#IBOutlet weak var newspaperImage: UIImageView!
func imageGradient(){
let newspaperview = UIView(frame: newspaperImage.frame)
let gradient = CAGradientLayer()
gradient.frame = newspaperview.bounds
let startColor = UIColor(red: 30, green: 113, blue: 79, alpha: 0).cgColor
let endColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1).cgColor
gradient.colors = [startColor, endColor]
newspaperview.layer.insertSublayer(gradient, at: 0)
newspaperImage.addSubview(newspaperview)
newspaperImage.bringSubviewToFront(newspaperview)
}
override func viewDidLoad() {
super.viewDidLoad()
imageGradient()
}
After running, I find it doesn't work at all. Where's the problem?
First lets start with UIColor(red:green:blue:alpha) expects the parameters to be normalised values of 0-1.
So...
let startColor = UIColor(red: 30, green: 113, blue: 79, alpha: 0).cgColor
should be
let startColor = UIColor(red: 30/255, green: 113/255, blue: 79/255, alpha: 0).cgColor
Next, I took your basic code and dumped into Playground
let image = UIImage(named: "Miho_Small.png")
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
imageView.contentMode = .scaleAspectFit
imageView.image = image
let gradient = CAGradientLayer()
gradient.frame = imageView.bounds
let startColor = UIColor(red: 30/255, green: 113/255, blue: 79/255, alpha: 0).cgColor
let endColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1).cgColor
gradient.colors = [startColor, endColor]
imageView.layer.insertSublayer(gradient, at: 0)
imageView
And it generated...
Okay, that's not what I was expecting, but it's not entirely unsurprising. The CALayer is been painted over the top of the image, because the image is been painted by the UIView's draw function.
Okay, so how can we fix it? With out going to the extend of making a new images and painting the gradient and image into it, you could make use of a "background" UIView, onto which the layer and UIImageView are applied...
let image = UIImage(named: "Miho_Small.png")
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
imageView.contentMode = .scaleAspectFit
imageView.image = image
imageView.backgroundColor = nil
let backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
let gradient = CAGradientLayer()
gradient.frame = backgroundView.bounds
let startColor = UIColor(red: 30/255, green: 113/255, blue: 79/255, alpha: 0).cgColor
let endColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1).cgColor
gradient.colors = [startColor, endColor]
backgroundView.layer.insertSublayer(gradient, at: 0)
backgroundView.addSubview(imageView)
backgroundView
which generates...
And, that's probably more along the lines of what you're trying to achieve...at a guess
:(, the two views can not be overlapped
I don't "overlap", so to speak, I add the UIImageView onto the backgroundView, which contains the CALayer. Remember, the view's frame is relative to it's parent's coordinate space.
In almost all cases, I prefer to use auto layout, and I prefer to use story boards, but those are difficult to make int an answer, so I've done it by hand for this example...
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let image = UIImage(named: "Miho_Small.png")
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 250, height: 250))
imageView.contentMode = .scaleAspectFit
imageView.image = image
imageView.backgroundColor = nil
let backgroundView = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 250))
backgroundView.translatesAutoresizingMaskIntoConstraints = false
let gradient = CAGradientLayer()
gradient.frame = backgroundView.bounds
let startColor = UIColor(red: 30/255, green: 113/255, blue: 79/255, alpha: 0).cgColor
let endColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1).cgColor
gradient.colors = [startColor, endColor]
backgroundView.layer.insertSublayer(gradient, at: 0)
backgroundView.addSubview(imageView)
view.addSubview(backgroundView)
imageView.leadingAnchor.constraint(equalTo: backgroundView.leadingAnchor).isActive = true
imageView.trailingAnchor.constraint(equalTo: backgroundView.trailingAnchor).isActive = true
imageView.topAnchor.constraint(equalTo: backgroundView.topAnchor).isActive = true
imageView.bottomAnchor.constraint(equalTo: backgroundView.bottomAnchor).isActive = true
backgroundView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
backgroundView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
backgroundView.addConstraint(NSLayoutConstraint(item: backgroundView,
attribute: .width,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1.0,
constant: 250))
backgroundView.addConstraint(NSLayoutConstraint(item: backgroundView,
attribute: .width,
relatedBy: .equal,
toItem: backgroundView,
attribute: .height,
multiplier: 1.0,
constant: 0))
}
}
For what it's worth, you can checkout the project I used to test the code from GitHub - TestCALayerGradientAndImageOverlay

Swift : Set anchor Point on CABasicAnimation

The code you see below creates a CALayer (rectangle shape) and animates it from the left to the right when the user holds down on the screen ('longPressGestureRecognizer'). When they lift their finger, the CALayer stops animating, it gets pushed into an array, and when they hold on the screen again another CALayer is created. You can copy and paste code directly in new project:
//Global Variables
var layer: CALayer?
var holdGesture = UILongPressGestureRecognizer()
let animation = CABasicAnimation(keyPath: "bounds.size.width")
var layerHolder = [CALayer]()
var widthIndex = CGPoint(x: 0, y: 0)
var nextXOffset = CGFloat(0.0)
var checkIfFull = CGFloat()
var colorIndex : Int = 0
let barColors = [
//Red
UIColor(red: 0.969, green: 0.49, blue: 0.443, alpha: 1),
//Orange
UIColor(red: 0.984, green: 0.647, blue: 0.431, alpha: 1),
//Pink
UIColor(red: 0.894, green: 0.592, blue: 0.698, alpha: 1),
//Purple
UIColor(red: 0.851, green: 0.6, blue: 0.957, alpha: 1),
//Yellow
UIColor(red: 0.98, green: 0.875, blue: 0.455, alpha: 1),
//Green
UIColor(red: 0.49, green: 0.792, blue: 0.616, alpha: 1),
//Blue
UIColor(red: 0.553, green: 0.71, blue: 0.906, alpha: 1)]
func setUpView(){
self.view.addGestureRecognizer(holdGesture)
holdGesture.addTarget(self, action:"handleLongPress:")
}
func handleLongPress(sender : UILongPressGestureRecognizer){
if(sender.state == .Began) {
let newLayer = CALayer()
newLayer.frame = CGRect(x: nextXOffset, y: 0, width: 0, height: 10)
newLayer.backgroundColor = barColors[colorIndex % 7].CGColor
print("before \(nextXOffset)")
newLayer.anchorPoint = widthIndex
animation.fromValue = 0
animation.toValue = self.view.bounds.width * 2 - nextXOffset
animation.duration = 5
self.view.layer.addSublayer(newLayer)
print("Long Press Began")
newLayer.addAnimation(animation, forKey: "bounds.size.width")
layer = newLayer
}
else {
print("Long press ended")
if let layer = layer {
print("after \(nextXOffset)")
pauseLayer(layer)
layer.frame = layer.presentationLayer()!.frame
nextXOffset = CGRectGetMaxX(layer.frame)
layerHolder.append(layer)
colorIndex++
}
}
}
My issue is that when the new CALayer is created and animating, it grows from both sides not just growing to the right. I need them to be side by side to each other, one moving to the right where the other one left off. I didn't notice it until i had them interchange colors. After some research, i believe setting an anchor point from where the last CALayer left off is the correct approach. Ive been trying to achieve this but haven't been able to do it. Help me? Please?
UPDATE : I believe if I was to use the nextXOffset as the anchor point, I may be able to do this, but it is of type CGfloat not CGPoint. Help please!
This works, Im so dumb. Keeping the 'widthIndex' at (0,0) allows it to start where the last bar left off. The code works.

Add Gradient on a view in Swift

The following code seems to work using Objective-C however its Swift version doesn't work.
I have tried the following:
Add the gradient inside the collection view cell
Add the gradient in the viewController
Use UIColor & CGColor
Use insertSubLayer
var mGradient = CAGradientLayer()
mGradient.frame = favoriteCell.mImageView.bounds
var colors = [CGColor]()
colors.append(UIColor(red: 0, green: 0, blue: 0, alpha: 1).CGColor)
colors.append(UIColor(red: 0, green: 0, blue: 0, alpha: 0).CGColor)
mGradient.startPoint = CGPointMake(0.1, 0.5)
mGradient.endPoint = CGPointMake(0.9, 0.5)
favoriteCell.mImageView.layer.addSublayer(mGradient)
Swift 3
let mGradient = CAGradientLayer()
mGradient.frame = favoriteCell.mImageView.bounds
var colors = [CGColor]()
colors.append(UIColor(red: 0, green: 0, blue: 0, alpha: 1).cgColor)
colors.append(UIColor(red: 0, green: 0, blue: 0, alpha: 0).cgColor)
mGradient.startPoint = CGPoint(x: 0.1, y: 0.5)
mGradient.endPoint = CGPoint(x: 0.9, y: 0.5)
mGradient.colors = colors
favoriteCell.mImageView.layer.addSublayer(mGradient)
You need to set set the colors of the gradient
mGradient.colors = colors
Try this one:
var mGradient : CAGradientLayer = CAGradientLayer()
mGradient.frame = favoriteCell.mImageView.bounds
mGradient.frame.origin = CGPointMake(0.0,0.0)
var colors = [CGColor]()
colors.append(UIColor(red: 0, green: 0, blue: 0, alpha: 1).CGColor)
colors.append(UIColor(red: 0, green: 0, blue: 0, alpha: 0).CGColor)
mGradient.locations = [0.0 , 1.0]
mGradient.startPoint = CGPointMake(0.1, 0.5)
mGradient.endPoint = CGPointMake(0.9, 0.5)
mGradient.frame = CGRect(x: 0.0, y: 0.0, width: favoriteCell.mImageView.frame.size.width, height: favoriteCell.mImageView.frame.size.height)
favoriteCell.mImageView.layer.insertSublayer(mGradient, atIndex: 0)

Gradiant in playground different from simulator

I tried to make a gradiant using playground then make it into a subclass of uiview to use it in my codebase.
The playground code is like this:
let view = UIView(frame: CGRect(x: 0, y: 0, width: 640, height: 41
))
let gradient: CAGradientLayer = CAGradientLayer(layer: view.layer)
gradient.frame = view.frame
let lightColor = UIColor(red: 157/256, green: 157/256, blue: 154/256, alpha: 1)
let darkColor = UIColor(red: 108/256, green: 104/256, blue: 104/256, alpha: 1)
gradient.colors = [lightColor.CGColor, darkColor.CGColor]
gradient.locations = [0.6 , 0.4]
gradient.startPoint = CGPoint(x: 1, y: 0.55)
gradient.endPoint = CGPoint(x: 0, y: 0.45)
view.layer.insertSublayer(gradient, atIndex: 0)
view //displayed
And display like this:
I tried to make it into my codebase with:
class GradiantView : UIView {
override var frame: CGRect {
didSet {
let gradient: CAGradientLayer = CAGradientLayer(layer: layer)
gradient.frame = bounds
let lightColor = UIColor(red: 157/256, green: 157/256, blue: 154/256, alpha: 1)
let darkColor = UIColor(red: 108/256, green: 104/256, blue: 104/256, alpha: 1)
gradient.colors = [lightColor.CGColor, darkColor.CGColor]
gradient.locations = [0.6 , 0.4]
gradient.startPoint = CGPoint(x: 1, y: 0.55)
gradient.endPoint = CGPoint(x: 0, y: 0.45)
layer.insertSublayer(gradient, atIndex: 0)
}
}
}
However, the result is like this: (no gradiant applied)
What happened and how to fix it ?
I inverted the locations and it seems to work.
I welcome any explanation on the difference between simulator and playground.
gradientLayer.locations = [0.4 , 0.6]

Move coordinates of UIView to LLO with Swift

I would expect this code to move my newLabel from the upper left corner (ULO) to the lower left corner (LLO) but it doesn't! Apparently I need someone to hold my hand through the process of setting (0, 0) to LLO for a UIView. I've been through the documentation and read post after post on this forum but can't seem to make it happen. Disclaimer: I am trying to accomplish this in a playground in xCode 6 beta 2 so it possible it's a bug or not supported.
var newLabel = UILabel(frame: CGRectMake(0, 0, 150, 50))
newLabel.text = "This is a test"
newLabel.layer.borderWidth = 2
newLabel.textAlignment = NSTextAlignment.Center
newLabel.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.8)
var myView = UIView(frame: CGRectMake(0, 0, 200, 100))
myView.layer.borderWidth = 2
myView.addSubview(newLabel)
myView.transform = CGAffineTransformMakeScale(1, -1)
myView.backgroundColor = UIColor(red: 0.8, green: 0, blue: 0, alpha: 0.8)
It seems like you haven't added Myview in main view try this code:--
var newLabel = UILabel(frame: CGRectMake(0, 0, 150, 50))
newLabel.text = "This is a test"
newLabel.layer.borderWidth = 2
newLabel.textAlignment = NSTextAlignment.Center
newLabel.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.8)
var myView = UIView(frame: CGRectMake(0, 0, 200, 100))
myView.layer.borderWidth = 2
myView.addSubview(newLabel)
myView.transform = CGAffineTransformMakeScale(1, -1)
myView.backgroundColor = UIColor(red: 0.8, green: 0, blue: 0, alpha: 0.8)
self.view.addSubview(myView);
To flip the UIView it has to be a subView of another view... My issue was I was trying to flip the mainView or baseView which apparently cant be done. This make since, since it needs a graphic space to be able to draw to. Below is the code achieving what I was looking for. Also of not I also had to "flip" my label as well to return it to it upright state (again this is logical).
var newLabel = UILabel(frame: CGRectMake(0, 0, 150, 50))
newLabel.transform = CGAffineTransformMakeScale(1, -1)
newLabel.text = "This is a test"
newLabel.layer.borderWidth = 2
newLabel.textAlignment = NSTextAlignment.Center
newLabel.backgroundColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 0.8)
var subView = UIView(frame: CGRectMake(50, 50, 200, 100))
subView.transform = CGAffineTransformMakeScale(1, -1)
subView.layer.borderWidth = 2
subView.addSubview(newLabel)
subView.backgroundColor = UIColor(red: 0.8, green: 0, blue: 0, alpha: 0.8)
var myView = UIView()
myView = UIView(frame: CGRectMake(0, 0, 300, 200))
//myView.transform = CGAffineTransformMakeScale(1, -1)
myView.layer.borderWidth = 2
myView.addSubview(subView)
myView.backgroundColor = UIColor(red: 0, green: 0.8, blue: 0, alpha: 0.8)
Thanks #Mohit for your input in helping me to find the solution.

Resources