Iv been working on this new game for a couple months now and run into a problems I cant figure out!
So basically I am creating a "status section" in the top right of my UIViewController. In this "status section" will appear UILabels that hold status updates as Strings. These UILabels will appear at the very top left of the UIView and will go slowly downwards and lose Alpha at the same time..eventually dissappearing once it reaches the bottom.
What Iv Done?
So as you can see in the code before so far Iv been able to create the UILabel at random locations but for some reason the UILabels spawn off screen.
Code
func createStatus() {
let viewWidth = self.statusView.frame.width
let viewHeight = self.statusView.frame.width
let randomX = CGFloat(viewWidth / 2)
let randomY = CGFloat(arc4random_uniform(UInt32(viewHeight)) + 50)
let frame = self.statusView.frame.origin
let status = UILabel(frame: CGRect(origin: CGPoint(x: CGFloat(viewWidth / 4) - 20, y: CGFloat(viewHeight - viewHeight) + 10), size: CGSize(width: 200.0, height: 50.0)))
status.text = "This is a status"
status.textColor = UIColor.redColor()
status.alpha = 1
self.statusView.addSubview(status)
print("Status made")
UIView.animateWithDuration(2) {
status.center = frame
status.alpha = 0
}
}
Iv added an image of the problem and the UIViewstatusView as well. Im having trouble making the UILabel at the top left and i'v tried this code below and it doesnt even "spawn" the UILabel on the screen.
let frame = self.statusView.frame.origin
CGPoint topLeft = CGPointMake(CGRectGetMinX(frame), CGRectGetMinY(frame));
func createStatus() {
let viewWidth = self.statusView.frame.width
let viewHeight = self.statusView.frame.width
//Why are these here?
let randomX = CGFloat(viewWidth / 2)
let randomY = CGFloat(arc4random_uniform(UInt32(viewHeight)) + 50)
let origin = CGPointZero//Origins are relative to superview, so top left would be (0, 0)
//Renamed status to statusLabel so that we know its a label and not a model object
let statusLabel = UILabel(frame: CGRect(origin: origin, size: CGSize(width: 200.0, height: 50.0)))
statusLabel.text = "This is a status"
statusLabel.textColor = UIColor.redColor()
statusLabel.alpha = 1
self.statusView.addSubview(statusLabel)
print("Status made")
UIView.animateWithDuration(2) {
//Animate the origin instead of center position.
//The origin will be the height of the statusView - the height of your label. x stays 0 to keep it along left edge.
statusLabel.frame.origin = CGPoint(x: 0, y: self.statusView.frame.height - statusLabel.frame.height )
statusLabel.alpha = 0
}, { (completed) in
if completed {
//If statusLabel doesn't have a superView it will be dealloc after the function goes out of scope. So remove it from superview after animaion is complete and ARC takes care of it.
statusLabel.removeFromSuperview()
}
}
}
Related
I have created this width animation which creates a border-bottom for my TextField and I want to animate it so that when the user clicks on the TextField the CALayer goes from left to right and then appear. the problem is that the animation start from the center and then the width grows to each side while I want it to start from left to right... What am I missing ?
override func didAddSubview(_ subview: UIView) {
let shape = CALayer()
shape.frame = CGRect(x: 0, y: self.height, width: self.width, height: 1)
shape.frame.origin.x = 0
shape.backgroundColor = UIColor.red.cgColor
self.layer.addSublayer(shape)
let boundsAnim:CABasicAnimation = CABasicAnimation(keyPath: "bounds.size.width")
boundsAnim.fromValue = 0
boundsAnim.toValue = self.width
boundsAnim.duration = 2
shape.add(boundsAnim, forKey: "bounds.size.width")
}
Set the anchor point to the left side
shape.anchorPoint = CGPoint(x: 0, y: 0.5)
I have been stuck/ researching this problem for days, the method below successfully creates the pdf from a regular size UIView. However, I need the contents of my containerView that is located inside a UIScrollview. The height of my containerView is 5000. Can someone help me to render the containerView into multiple PDF pages or direct me to a better way of doing this. `func exportAsPdfFromView() -> String {
func screenShotScrollview() -> String {
let scrollView = waiverViews.scrollView
let pageDimensions = scrollView.bounds
let pageSize = pageDimensions.size
let totalSize = scrollView.contentSize
let numberOfPagesThatFitVertivally = Int(ceil(totalSize.height/pageSize.height))
let outputData = NSMutableData()
UIGraphicsBeginPDFContextToData(outputData, pageDimensions, nil)
let savedContentOffset = scrollView.contentOffset
let savedContentInset = scrollView.contentInset
scrollView.contentInset = UIEdgeInsets.zero
let origin = CGRect(x: 0, y: 0, width: pageSize.width, height: pageSize.height)
if let context = UIGraphicsGetCurrentContext() {
for indexVertical in 0..<numberOfPagesThatFitVertivally {
UIGraphicsBeginPDFPageWithInfo(origin, nil)
let offsetVertical = CGFloat(indexVertical) * pageSize.height
scrollView.contentOffset = CGPoint(x: 0, y: offsetVertical)
context.translateBy(x: 0, y: -offsetVertical)
scrollView.layer.render(in: context)
}
UIGraphicsEndPDFContext()
scrollView.contentInset = savedContentInset
scrollView.contentOffset = savedContentOffset
}
return self.saveViewPdf(data: outputData)
}
Essentially, a scrollView would not want to render all contents to its layer all the time, until it is being viewed.
I am guessing this is returning just the snapshot of the UIScrollView's current window?
To take multiple snapshots of the whole contentBounds of the UIScrollView, you could potentially, setTheContentOffset of the scrollview in its total contentBounds.height/frame.height which would return the pages. And then generate PDFs within the loop?
for instance something like this, and then UX wise, hide the scroll view. While this is being generated, unhide it when it is complete.
for i in 0..<pages {
scrollView.setContentOffset(.init(x: 0, y: i*scrollViewHeight))
//PDF Generation flow
//set the url to something like "pdf_file_\(i).pdf"
}
Trying to show buttons dynamically in a UICollectionViewCell depends on data coming in array from api. Below is what trying to achieve:
This is so far what i have achieved using button, but i need multiple buttons means button next to "About us" then next until space there then move to next line and setting height of cell as per height of view in cell. Please guide how it can be achieved.
Below is my code:
var xPosition = cell.btnOptions.frame.origin.x
var yPosition = cell.btnOptions.frame.origin.y
var width = cell.btnOptions.frame.size.width
var height = cell.btnOptions.frame.size.height
if((arrOptions?.count ?? 0) > 0)
{
for i in 0..<arrOptions!.count
{
//
cell.btnOptions.tag = i
cell.btnOptions.layer.cornerRadius = 3.0
cell.btnOptions.frame = CGRect.init(x: xPosition, y: yPosition, width: width, height: height)
//
cell.btnOptions.setTitle(arrOptions?.objectAt(i).object(forKey: "title") as? String, for: .normal)
xPosition = width + width + 8
yPosition = height
}
In my app I want to change the titleView of my Navigation Controller to a custom ImageView but I have a very strange problem. I basically just can`t set the frame of my ImageView.
This is my code:
func NavBarEinrichten(){
let logo = UIImage(named: "Notennamen für Griffe 1")!
let imageView = UIImageView(image:logo)
imageView.contentMode = .scaleAspectFit
//To show the problem better
imageView.backgroundColor = UIColor.red
let navController = navigationController!
let bannerWidth:CGFloat = 20
let bannerHeight:CGFloat = navController.navigationBar.frame.size.height
let bannerX = bannerWidth / 2 - logo.size.width / 2
let bannerY = bannerHeight / 2 - logo.size.height / 2
imageView.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
self.navigationItem.titleView?.frame = CGRect(x: bannerX, y: bannerY, width: bannerWidth, height: bannerHeight)
navigationItem.titleView = imageView
print(imageView.frame, "imageView frame")
}
The print statement prints:
(0.0, 0.0, 357.0, 142.0) imageView frame
Which is right, because it looks like this when i run it, but not like it should be. Because I want the width to be 20.
In the simulator it looks like this:
I tried to call this method in viewDidLoad(), viewWillAppear()and viewDidLayoutSubviews()
Every time with the exact same outcome...
Can you help me?
I've been trying to make a zoom animation like Facebook when you click a picture into a cell to move into the middle of the screen. The animation works, but for a reason that I can not figure out, it is not starting from the initial position it is giving me another frame. Please help, I've been struggling with this for a few days now.
I am using a collectionView with CustomCell and everything it's done programmatically:
The function in CenterVC:
//MARK: Function to animate Image View (it will animate to the middle of the View)
func animateImageView(statusImageView : UIImageView) {
//Get access to a starting frame
statusImageView.frame.origin.x = 0
if let startingFrame = statusImageView.superview?.convert(statusImageView.frame, to: nil) {
//Add the view from cell to the main view
let zoomImageView = UIView()
zoomImageView.backgroundColor = .red
zoomImageView.frame = statusImageView.frame
view.addSubview(zoomImageView)
print("Starting frame is: \(startingFrame)")
print("Image view frame is: \(statusImageView.frame)")
UIView.animate(withDuration: 0.75, animations: {
let height = (self.view.frame.width / startingFrame.width) * startingFrame.height
let y = self.view.frame.height / 2 - (height / 2)
zoomImageView.frame = CGRect(x: 0, y: y, width: self.view.frame.width, height: height)
})
}
}
This is the pictureView inside the cell and the constraints (this is where I am setting up the picture for the view, and I am using in cellForRowAtIndexPath cell.centerVC = self):
var centerVC : CenterVC?
func animate() {
centerVC?.animateImageView(statusImageView: pictureView)
}
let pictureView : UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFill
imageView.image = UIImage(named: "cat")
imageView.backgroundColor = UIColor.clear
imageView.layer.masksToBounds = true
imageView.isUserInteractionEnabled = true
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
addConstraintsWithFormat(format: "V:|-5-[v0(40)]-5-[v1]-5-[v2(200)]", views: profileImage, postTextView, pictureView)
addConstraintsWithFormat(format: "H:|[v0]|", views: pictureView)
This is what it prints out in the debugger:
Starting frame is: (5.0, 547.5, 365.0, 200.0)
Image view frame is: (0.0, 195.5, 365.0, 200.0)
As you can see the starting frame it's different from the initial frame and position of the picture. The animation it's not leaving the initial position it just appears somewhere on top and animates to the middle. I don't know what to do, please advice.
For some reason, it was not reading the starting frame rect so I've made a new CGRect that gets the origin and set the size of the picture: (it animates perfectly now)
zoomImageView.frame = CGRect(origin: startingFrame.origin, size: CGSize(width: view.frame.width, height: statusImageView.frame.height))