How to add view in center of view - ios

I have view which has 200x200 width and height
let pulsator = Pulsator()
pulsator.radius = 240.0
pulsator.numPulse = 5
viewSearchArea.layer.addSublayer(pulsator)
pulsator.start()
I want pulsator will be set center of viewSearchArea.
How could i do this?
If anyone wants to know what is pulsator here is the link

Try this.
pulsator.position = CGPoint(viewSearchArea.layer.bounds.midX, viewSearchArea.layer.bounds.midY)

Set your frame like this:
let height:CGFloat = 200.0
let width:CGFloat = 200.0
let xPosition = (self.view.frame.size.width/2) - (width/2);
let yPosition = (self.view.frame.size.height/2) - (height/2);
let pulsator = UIView()
pulsator.frame = CGRect.init(x:xPosition , y: yPosition, width: width, height: height)
Another option to set center:
pulsator.center = self.view.center;

I found this in the example project:
pulsator.position = viewSearchArea.layer.position
Try that or this:
pulsator.frame = CGRect(x: (viewSearchArea.frame.width/2) - 100, y: (viewSearchArea.frame.height/2) - 100, width: 200, height: 200)

Related

UITableView - UITableViewCell.Style .subtitle - How to make UIImageView circular? - Swift 5

I got a UITableView with default cell styles. (set to .subtitle style)
I want to make its UIImageView circular.
cell.imageView.layer.borderWidth = 1
cell.imageView.layer.masksToBounds = false
cell.imageView.layer.borderColor = UIColor.white.cgColor
cell.imageView.layer.cornerRadius = profileImageView.frame.size.width/2
cell.imageView.clipsToBounds = true
this would not work at this situation!
I found the right way thanks to https://stackoverflow.com/a/50462058/10489699 .
let itemSize = CGSize.init(width: 50, height: 50)
UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.main.scale)
let imageRect = CGRect.init(origin: CGPoint.zero, size: itemSize)
cell.imageView?.image!.draw(in: imageRect)
cell.imageView?.image! = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
cell.imageView?.layer.cornerRadius = (itemSize.width) / 2
cell.imageView?.clipsToBounds = true
Don't try if let image = cell.imageView?.image! {...} , I don't know why but it hits the UIImageView !
you can use this code
public extension UIView {
func round() {
let width = bounds.width < bounds.height ? bounds.width : bounds.height
let mask = CAShapeLayer()
mask.path = UIBezierPath(ovalIn: CGRect(x: bounds.midX - width / 2, y: bounds.midY - width / 2, width: width, height: width)).cgPath
self.layer.mask = mask
}
}
and you can use like this:
profileImageView.round()

Place view in the bottom right, hiding part, programmatically swift 5

I want to move the view as shown in the image programmatically, to obtain an effect similar to 1/4 of roulette.
But I only get it to show below as indicated in the code for one screen.
let X_Position:CGFloat? = 150.0 //use your X position here
let Y_Position:CGFloat? = 300.0 //use your Y position here
circle.frame = CGRect(x: X_Position ?? 0,y: Y_Position ?? 0,width:
circle.frame.width,height: circle.frame.height)
How can I get this position for all screens?
Swift 4
Without Animation:
let screen = UIScreen.main.bounds
let circleViewRect = circlemenu.frame
circlemenu.frame = CGRect.init(x: screen.width - circleViewRect.width/2, y: screen.height - circleViewRect.height/2, width: circleViewRect.width, height: circleViewRect.height)
With Animation:
let screen = UIScreen.main.bounds
let circleViewRect = circlemenu.frame
UIView.animate(withDuration: 0.3) {
circlemenu.frame = CGRect.init(x: screen.width - circleViewRect.width/2, y: screen.height - circleViewRect.height/2, width: circleViewRect.width, height: circleViewRect.height)
}
I believe you are using an old version of swift, so feel free to change the functions accordingly

Why cannot I put this card on a UIScrollView correctly?

I have specified the frame of a Card, why can't I add this card to a scrollView?
When I try to add it to a UIScrollView, it will just become a withe bar. Cannot display the content(which is a label), or the toolbar(with title and detail and an IconButton).
Code for scrollView
eventList = UIScrollView()
eventList?.frame = CGRect(x: 0, y: (calender?.frame.height)! + (calenderMenu?.frame.height)!, width: ScreenWidth, height: ScreenHeight - (calenderMenu?.frame.height)! - (calender?.frame.height)!)
eventList?.scrollsToTop = true
eventList?.isScrollEnabled = true
eventList?.bounces = true
eventList?.isPagingEnabled = true
eventList?.indicatorStyle = .default
eventList?.contentSize = CGSize(width: ScreenWidth - 20, height: 400)
eventList?.delegate = self
self.view.addSubview(eventList!)
Code for Card
signinCard = Card()
signinCard?.frame = view.bounds
signinCard?.toolbar = signinToolbar
signinCard?.toolbarEdgeInsetsPreset = .square3
signinCard?.toolbarEdgeInsets.bottom = 0
signinCard?.toolbarEdgeInsets.right = 8
signinCard?.contentView = contentView
signinCard?.contentViewEdgeInsetsPreset = .wideRectangle3
signinCard?.bottomBar = Bar()
signinCard?.frame = CGRect(x: 10, y: 10, width: ScreenWidth - 20, height: (eventList?.frame.height)! / 2 - 5)
eventList?.addSubview(signinCard!)

My scrollView cut my last picture Swift 3

I have an horizontal scrollView, where I add my array of image, but it alway cut the picture, my content mode is scaleToFill. I printed the size of my imageView and my scrollView and gave me the same size, so why can't I see my picture filling all my scrollView
My first Image and
My last image of my array
Here is my code
func getFavoriteSalon()
{
arrayOfDataScrollView = [#imageLiteral(resourceName: "large"), #imageLiteral(resourceName: "Kristoff-image-kristoff-36081750-500-266"), #imageLiteral(resourceName: "image-battle-for-dream-island-39780938-500-266")]
mainScrollView.contentSize.width = (mainScrollView.frame.width) * CGFloat(arrayOfDataScrollView.count)
for i in 0..<arrayOfDataScrollView.count
{
let imageView = UIImageView()
let xPosition = self.mainScrollView.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.mainScrollView.frame.width , height: self.mainScrollView.frame.height)
imageView.image = arrayOfDataScrollView[i]
imageView.contentMode = .scaleToFill
print(self.mainScrollView.frame.width) =>Give me 375
print(self.mainScrollView.frame.height) =>Give me 200
print("Width => \(imageView.frame.width)") =>Give me 375
print("height => \(imageView.frame.height)") =>Give me 200
mainScrollView.addSubview(imageView)
}
}
I found a solution, for me it's a bug xcode, i work with an emulateur of Iphone7+, but I got the value of an Iphone 7 when i print my variables, Be sure to have the preview, and the emulateur looking the same device
Use this and set content size according image
var xPosition:CGFloat = 0
for i in 0..<arrayOfDataScrollView.count{
var frame: CGRect = CGRect(x: 0, y: 0, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)
frame.origin.x = CGFloat(xPosition)
frame.size = mainScrollView.frame.size
let imageView = UIImageView(frame: frame)
imageView.contentMode = .scaleToFill
imageView.frame.size.width = imageWidth
imageView.frame.size.height = imageHeight
imageView.frame.origin.x = xPosition
imageView.frame.origin.y = 0
mainScrollView.addSubview(imageView)
xPosition += imageView.frame.size.width + 5; (5 you can change this value)
mainScrollView.contentSize = CGSize(width: CGFloat(i) * imageView.frame.size.width, height:
mainScrollView.frame.size.height)
}

Swift UITextField Border bottom and shadow issues

I have a UITextField that must have a bottom border. Since autolayout use my code is this:
override func viewDidLayoutSubviews() {
let border = CALayer()
let width = CGFloat(0.5)
border.borderColor = UIColor.darkGray.withAlphaComponent(0.3).cgColor
border.frame = CGRect(x: 0, y: txtUsername.frame.size.height - width, width: txtUsername.frame.size.width, height: txtUsername.frame.size.height)
border.borderWidth = width
txtUsername.layer.addSublayer(border)
txtUsername.layer.masksToBounds = true
}
This, however, excludes the fact that when I touch the UITextField, this should create me a shadow. the code is this:
//ombra txtField
self.view.addSubview(txtUsername)
txtUsername.layer.shadowOffset = CGSize(width: 5, height: 10)
txtUsername.layer.shadowOpacity = 0.3
txtUsername.layer.shadowRadius = 10
txtUsername.layer.masksToBounds = false;
txtUsername.clipsToBounds = false
the one excludes the other. how do I fix?`
You can use CAShapeLayerShadow
func border(){
let border = CALayer()
let width = CGFloat(0.5)
border.borderColor = UIColor.darkGray.withAlphaComponent(1).cgColor
border.frame = CGRect(x: 0, y: CustomerTextBox.frame.size.height - width, width: CustomerTextBox.frame.size.width, height: CustomerTextBox.frame.size.height)
border.borderWidth = width
CustomerTextBox.layer.addSublayer(border)
CustomerTextBox.layer.masksToBounds = true
}
func shadow(){
let Shape = CAShapeLayer()
let myPath = UIBezierPath(ovalIn: CustomerTextBox.frame)
Shape.shadowPath = myPath.cgPath
Shape.shadowColor = UIColor.lightGray.cgColor
Shape.shadowOffset = CGSize(width: 5, height: 3)
Shape.shadowRadius = 5
Shape.shadowOpacity = 0.8
view.layer.insertSublayer(Shape, at: 0)
}

Resources