Programmatically creating Label that shows Slider value on each scene on ScrollView - ios

im kinda new to the programming world specifically on swift.
Ive started to practice a bit and im currently working on a demo app just to explore..
So right now what the app suppose to do is to show have a scroll view, and each screen shows a different "animal" or any other subject by an Image, a Label and an Icon, on the side theres a slider with 1-5 values which suppose to show how much you "love" that subject on a scale of 1 to 5.
everything works except I cant seem to add a label to each of the screens above the slider to show its current value.
with the current code it shows every screen with its appropriate image/label/icon, shows the right Page on the page indicator, and shows a working Slider,
only on the last frame the Slider have a Label that is connected to its value.
cant seem to figure it out
hopefully its clear enough, thanks alot.
(P.S Im fully aware that i might be doing this thing completely wrong in the first place, enlighten me its welcome)
class ViewController: UIViewController, UIScrollViewDelegate {
public var sliderLbl = UILabel()
#IBOutlet var mainScrollView: UIScrollView!
#IBOutlet var secondScrollView: UIScrollView!
var imageArray = [UIImage]()
var iconImg = [UIImage]()
var pageControl : UIPageControl = UIPageControl(frame: CGRect(x: 160, y: 420, width: 50, height: 100))
var subjects:[String] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
mainScrollView.delegate = self
mainScrollView.frame = view.frame
imageArray = [pic1.png, pic2.png, pic3.png, pic4.png, pic5.png]
subjects = ["Dog", "Cat", "Mouse", "Cow", "Snake"]
iconImg = [icon1.png, icon2.png, icon3.png, icon4.png]
for i in 0..<imageArray.count{
let labelView = UILabel()
let imageView = UIImageView()
let subjIcon = UIImageView()
let oneSlider = UISlider()
let sliderLabel = sliderLbl
sliderLabel.textColor = UIColor.black
oneSlider.transform = CGAffineTransform(rotationAngle: CGFloat(-M_PI_2))
oneSlider.minimumValue = 1
oneSlider.maximumValue = 5
oneSlider.isUserInteractionEnabled = true
oneSlider.value = 1
oneSlider.tintColor = UIColor.darkGray
oneSlider.isContinuous = true
sliderLabel.text = "1"
labelView.text = subjects[i]
labelView.textColor = UIColor.darkGray
labelView.textAlignment = .center
imageView.alpha = 0.85
imageView.image = imageArray[i]
imageView.contentMode = .center
subjIcon.image = iconImg[i]
subjIcon.contentMode = .scaleAspectFit
let xPosition = self.view.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: -50, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)
labelView.frame = CGRect(x: xPosition, y: -250, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)
iconImg.frame = CGRect(x: xPosition, y: 500, width: self.mainScrollView.frame.width, height: 150)
oneSlider.frame = CGRect(x: xPosition, y: 250, width: 50, height: 130)
sliderLabel.frame = CGRect(x: xPosition+20, y: 180, width: self.mainScrollView.frame.width, height: 80)
mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
// Add subViews
mainScrollView.addSubview(subjIcon)
mainScrollView.addSubview(imageView)
mainScrollView.addSubview(labelView)
mainScrollView.bringSubview(toFront: subjIcon)
oneSlider.addTarget(self, action: #selector(sliderSlid(_:)) ,for: UIControlEvents.valueChanged)
mainScrollView.addSubview(sliderLabel)
mainScrollView.addSubview(oneSlider)
func configurePageControl() {
self.pageControl.numberOfPages = imageArray.count
self.pageControl.currentPage = 0
self.pageControl.tintColor = UIColor.red
self.pageControl.pageIndicatorTintColor = UIColor.lightGray
self.pageControl.currentPageIndicatorTintColor = UIColor.green
self.view.addSubview(pageControl)
}
configurePageControl()
}
}
func sliderSlid(_ sender: UISlider) {
sliderLbl.text = String(Int(roundf(sender.value)))
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
let pageNumber = round(mainScrollView.contentOffset.x / mainScrollView.frame.size.width)
pageControl.currentPage = Int(pageNumber)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

Related

Scroll view Does not show images but scrolls?

I am trying to learn the basics of IOS development and Ive been experimenting with different views.
I tried to make a scroll view that display an array of images which scrolls properly but none of the images appear?
import UIKit
class ViewController: UIViewController {
var imagesArray = [UIImage]()
let scrollView: UIScrollView = {
let scroll = UIScrollView()
scroll.isPagingEnabled = true
scroll.showsVerticalScrollIndicator = false
scroll.showsHorizontalScrollIndicator = false
scroll.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
return scroll
}()
override func viewDidLoad() {
super.viewDidLoad()
setScrollView()
}
func setScrollView(){
self.view.addSubview(scrollView)
imagesArray = [UIImage(named:"gamerrr"),UIImage(named:"family"),UIImage(named:"gamerrr"),UIImage(named:"family"),UIImage(named:"family")] as! [UIImage]
setUpImages(imagesArray)
}
func setUpImages(_ images:[UIImage]){
for i in 0..<images.count {
// container of each image
let imageView = UIImageView()
// shifting to next image
let xPos = UIScreen.main.bounds.width * CGFloat(i)
// width and height of the screen
imageView.frame = CGRect(x: xPos, y: 0, width: scrollView.frame.width, height: scrollView.frame.height)
imageView.contentMode = .scaleAspectFit
// shifting to next image
scrollView.contentSize.width = scrollView.frame.width * CGFloat(i+1)
scrollView.addSubview(imageView)
}
}
}
You may need to set the image
let imageView = UIImageView(image:images[i])

Horizontal ScrollView not fit its sub ImageViews?

I want horizontal scrollview with image view but image is not fitting with the scroll. i am doing this code but not getting the right thing
Here is my code
#IBOutlet weak var upperScroll: UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.isHidden = true
// Do any additional setup after loading the view.
var logoImage: [UIImage] = [
UIImage(named: "slider.png")!,
UIImage(named: "slider.png")!,
UIImage(named: "slider.png")!,
UIImage(named: "slider.png")!,
]
upperScroll.isScrollEnabled = true
let scrollWidth: Int = Int(self.view.frame.width)
upperScroll.contentSize = CGSize(width: CGFloat(scrollWidth), height:(self.upperScroll.frame.height))
upperScroll.frame = CGRect(x: 0, y: 51, width: upperScroll.frame.size.width, height: self.upperScroll.frame.height)
upperScroll.backgroundColor = UIColor.red
var xOffset: Int = 0
for index in 0..<logoImage.count {
let img = UIImageView(frame: CGRect(x: CGFloat(xOffset), y: 0, width: upperScroll.frame.size.width, height: self.upperScroll.frame.height))
img.image = logoImage[index]
upperScroll.addSubview(img)
xOffset += 100
}
view.addSubview(upperScroll)
upperScroll.contentSize = CGSize(width: CGFloat((scrollWidth + xOffset)), height: 110)
}
but i want this
Can anyone please help me? Thanks
Just remove your appdelegate code and add my fuction
also set scrollview constraint like leading,trailing,top,bottom Zero
func scrollImage() {
super.viewDidLoad()
self.navigationController?.navigationBar.isHidden = true
// Do any additional setup after loading the view.
var logoImage: [UIImage] = [
UIImage(named: "slider.png")!,
UIImage(named: "slider.png")!,
UIImage(named: "slider.png")!,
UIImage(named: "slider.png")!]
upperScroll.isPagingEnabled = true
upperScroll.isScrollEnabled = true
let scrollWidth: Int = Int(self.view.frame.width)
,
upperScroll.contentSize = CGSize(width: CGFloat(scrollWidth), height:(self.upperScroll.frame.height))
print(upperScroll.frame.size.width)
upperScroll.backgroundColor = UIColor.red
for index in 0..<logoImage.count {
let xPosition = self.view.frame.width * CGFloat(index)
upperScroll.layoutIfNeeded()
let img = UIImageView(frame: CGRect(x: CGFloat(xPosition), y: 0, width: upperScroll.frame.size.width, height: self.upperScroll.frame.height))
img.layoutIfNeeded()
img.image = logoImage[index]
upperScroll.addSubview(img)
}
view.addSubview(upperScroll)
upperScroll.contentSize = CGSize(width: CGFloat((scrollWidth) * logoImage.count), height: self.upperScroll.frame.height)
}
I hope it's save your time and working great
According to your requirement, the xOffset += 100 should be changed to xOffset += scrollWidth. And contents size set to:
upperScroll.contentSize = CGSize(width: CGFloat((scrollWidth * logoImage.count)), height: 110)
And remember to enable the pagination for scroll view.
First of all, you need two outlet for scroll view and PageControl,
var contentWidth:CGFloat = 0.0
// MARK: - Welcome screen with scroll view use page controll
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
scrollView.delegate = self
let myImages = ["intro1_1.png", "intro2_2.png", "intro3_3.png"] // images which loaded in assets
let imageWidth:CGFloat = view.frame.width
let imageHeight:CGFloat = view.frame.height
var xPosition:CGFloat = 0 //setup your position
var scrollViewSize:CGFloat=0 //setup your position
for image in myImages {
let myImage:UIImage = UIImage(named: image)!
let myImageView:UIImageView = UIImageView()
myImageView.image = myImage
myImageView.frame.size.width = imageWidth
myImageView.frame.size.height = imageHeight
myImageView.frame.origin.x = xPosition
myImageView.frame.origin.y = 0
scrollView.addSubview(myImageView)
xPosition += imageWidth
scrollViewSize += imageWidth
}
scrollView.contentSize = CGSize(width: scrollViewSize, height: imageHeight)
scrollView.contentSize = CGSize(width: view.frame.width * 3, height: view.frame.height) //here you setup how many pages in scroll will
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let pageWidth:CGFloat = scrollView.frame.width
let currentPage:CGFloat = floor((scrollView.contentOffset.x-pageWidth / 2)/pageWidth)+1
self.pageControll.currentPage = Int(currentPage)
}
hope, i help u

Slider determines gravity

So far I've created a rectangle that starts from the bottom and moves upward using UiDynamicAnimator. I would like the user to determine the "strength" of the negative gravity. I want the user to determine the value through a slider.
This is my code so far:
import UIKit
class ViewController: UIViewController {
var orangeSquare: UIView?
var animator: UIDynamicAnimator?
override func viewDidLoad() {
super.viewDidLoad()
func sliderChanged(sender: AnyObject) {
var sliderValue = sender.value
}
//Create animation
let dim = CGRectMake(100, 500, 200, 100)
orangeSquare = UIView(frame: dim)
orangeSquare?.backgroundColor = UIColor.orangeColor()
//Add item to the screen
self.view.addSubview(orangeSquare!)
//Initialize the animator
animator = UIDynamicAnimator(referenceView: self.view)
//Add gravity
let gravity = UIGravityBehavior(items: [orangeSquare!])
let direction = CGVectorMake(0.0, sliderValue)
gravity.gravityDirection = direction
//Collision
let boundries = UICollisionBehavior(items: [orangeSquare!])
boundries.translatesReferenceBoundsIntoBoundary = true
//Add animations
animator?.addBehavior(boundries)
animator?.addBehavior(gravity)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I get two errors:
"Ambiguous use of ´value´" and
"Use of unresolved identifier ´sliderValue´"
How do I convert ´sliderValue´ into a float with just one decimal point?
your code is missing a few things. sliderValue is an unresolved identifier because you have only declared it within sliderChanged but are referring to it in the main body of viewDidLoad. Also, I think that your use of value is ambiguous because you have declared the parameter to the function as AnyObject, whose value could be any one of a number of things!
Your code was missing a mechanism linking a change in the value of the slider with a change in the gravity behaviour. As such, I've implemented this using an explicit target attached to the slider object. I've also thrown in a label showing the magnitude of the gravitational force. This is quite rough but I think it achieves what you were looking to do.
import UIKit
class ViewController: UIViewController {
var dynamicAnimator : UIDynamicAnimator!
var gravityBehaviour : UIGravityBehavior!
var orangeSquare : UIView!
var slider : UISlider!
var sliderLabel : UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Subviews
self.orangeSquare = {
let oS : UIView = UIView(frame: CGRect(origin: CGPoint(x: (self.view.frame.width / 2) - 100, y: 500), size: CGSize(width: 200, height: 200)))
oS.backgroundColor = UIColor.orange
return oS
}()
self.slider = UISlider(frame: CGRect(origin: CGPoint(x: 100, y: 100), size: CGSize(width: self.view.frame.width - 400, height: 50)))
self.slider.addTarget(self, action: #selector(self.sliderValueDidChange), for: UIControlEvents.allTouchEvents)
self.slider.minimumValue = -5
self.slider.maximumValue = 5
self.slider.value = 0
self.sliderLabel = UILabel(frame: CGRect(origin: CGPoint(x: self.view.frame.width - 100, y: 100), size: CGSize(width : 50, height: 50)))
self.sliderLabel.backgroundColor = UIColor.red
self.sliderLabel.textAlignment = NSTextAlignment.center
self.sliderLabel.textColor = UIColor.white
self.sliderLabel.text = String(self.slider.value)
// Assemble
self.view.addSubview(self.orangeSquare)
self.view.addSubview(self.slider)
self.view.addSubview(self.sliderLabel)
// Configure dynamic behaviours
self.dynamicAnimator = UIDynamicAnimator(referenceView: self.view)
self.gravityBehaviour = UIGravityBehavior(items: [self.orangeSquare])
self.gravityBehaviour.gravityDirection = CGVector(dx: 0, dy: 0)
// Configure boundaries
let boundaries : UICollisionBehavior = UICollisionBehavior(items: [self.orangeSquare])
boundaries.translatesReferenceBoundsIntoBoundary = true
self.dynamicAnimator.addBehavior(self.gravityBehaviour)
self.dynamicAnimator.addBehavior(boundaries)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func sliderValueDidChange() {
// When the slider value changes, update the label text and the gravity vector
self.sliderLabel.text = String((round(self.slider.value) * 10) / 10)
self.gravityBehaviour.gravityDirection = CGVector(dx: 0, dy: CGFloat(-1 * self.slider.value))
}
}
Hope that helps. All best!
import UIKit
class ViewController: UIViewController {
var dynamicAnimator : UIDynamicAnimator!
var gravityBehaviour : UIGravityBehavior!
var orangeSquare : UIView!
var slider : UISlider!
var sliderLabel : UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Subviews
self.orangeSquare = {
let oS : UIView = UIView(frame: CGRect(origin: CGPoint(x: (self.view.frame.width / 2) - 100, y: 500), size: CGSize(width: 200, height: 200)))
oS.backgroundColor = UIColor.orangeColor()
return oS
}()
self.slider = UISlider(frame: CGRect(origin: CGPoint(x: self.view.frame.width - 100, y: 100), size: CGSize(width: self.view.frame.width - 300, height: 150)))
self.slider.addTarget(self, action: #selector(self.sliderValueDidChange), forControlEvents: UIControlEvents.AllTouchEvents)
self.slider.minimumValue = -10
self.slider.maximumValue = 10
self.slider.value = 0
self.sliderLabel = UILabel(frame: CGRect(origin: CGPoint(x: self.view.frame.width - 100, y: 200), size: CGSize(width : 50, height: 50)))
self.sliderLabel.backgroundColor = UIColor.redColor()
self.sliderLabel.textAlignment = NSTextAlignment.Center
self.sliderLabel.textColor = UIColor.whiteColor()
self.sliderLabel.text = String(self.slider.value)
// Assemble
self.view.addSubview(self.orangeSquare)
self.view.addSubview(self.slider)
self.view.addSubview(self.sliderLabel)
// Configure dynamic behaviours
self.dynamicAnimator = UIDynamicAnimator(referenceView: self.view)
self.gravityBehaviour = UIGravityBehavior(items: [self.orangeSquare])
self.gravityBehaviour.gravityDirection = CGVector(dx: 0.0, dy: 0.0)
// Configure boundaries
let boundaries : UICollisionBehavior = UICollisionBehavior(items: [self.orangeSquare])
boundaries.translatesReferenceBoundsIntoBoundary = true
self.dynamicAnimator.addBehavior(self.gravityBehaviour)
self.dynamicAnimator.addBehavior(boundaries)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func sliderValueDidChange() {
// When the slider value changes, update the label text and the gravity vector
self.sliderLabel.text = String((round(self.slider.value) * 10) / 10)
self.gravityBehaviour.gravityDirection = CGVector(dx: 0, dy: CGFloat(-1 * self.slider.value))
}
}

How to dynamically adjust height of UITextView

So I know there are a couple of post like mine, but not quite what I am looking for. So here is what I am trying to accomplish. I have this ViewController that looks like this.
When a user inputs text into the UITextView, which currently has the text "Description", I want to dynamically change the height of the UITextView so if the text where to become multiple lines or go off screen the user can scroll through the whole view with its contents. Here is my program.
#IBOutlet var scrollView: UIScrollView!
var projectNameTextField: UITextField!
var projectCategoryLabel: UIButton!
var dueDateLabel: UIButton!
var projectDescription: UITextView!
var projectDescriptionFrame: CGRect!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let scrollViewWidth = scrollView.frame.width
scrollView.contentSize.width = scrollViewWidth
let trallingSpace: CGFloat = 12.0
let contentWidth = scrollViewWidth - (trallingSpace * 2.0)
projectNameTextField = UITextField(frame: CGRect(x: trallingSpace, y: scrollViewContentHeight(), width: contentWidth, height: 40))
projectNameTextField.placeholder = "Title"
updateScrollView(withView: projectNameTextField)
let categoryLabel = UILabel()
categoryLabel.text = "Category"
projectCategoryLabel = UIButton()
projectCategoryLabel.setTitle("Art", forState: .Normal)
projectCategoryLabel.setTitleColor(UIColor.blackColor(), forState: .Normal)
let stackViewHCategory = UIStackView(frame: CGRect(x: trallingSpace, y: scrollViewContentHeight(), width: contentWidth, height: 40))
stackViewHCategory.axis = .Horizontal
stackViewHCategory.spacing = 8
stackViewHCategory.addArrangedSubview(categoryLabel)
stackViewHCategory.addArrangedSubview(projectCategoryLabel)
updateScrollView(withView: stackViewHCategory)
let dueDateTitle = UILabel()
dueDateTitle.text = "Due Date"
dueDateLabel = UIButton()
dueDateLabel.setTitle("No Due Date", forState: .Normal)
dueDateLabel.setTitleColor(UIColor.blackColor(), forState: .Normal)
let stackViewHDueDate = UIStackView(frame: CGRect(x: trallingSpace, y: scrollViewContentHeight(), width: contentWidth, height: 40))
stackViewHDueDate.axis = .Horizontal
stackViewHDueDate.spacing = 8
stackViewHDueDate.addArrangedSubview(dueDateTitle)
stackViewHDueDate.addArrangedSubview(dueDateLabel)
updateScrollView(withView: stackViewHDueDate)
projectDescription = UITextView(frame: CGRect(x: trallingSpace, y: scrollViewContentHeight(), width: contentWidth, height: 40))
projectDescription.text = "Description"
projectDescription.delegate = self
projectDescription.scrollEnabled = false
projectDescription.font = projectDescription.font?.fontWithSize(14)
updateScrollView(withView: projectDescription)
projectDescriptionFrame = projectDescription.frame
projectDescriptionFrame.size.height = projectDescription.contentSize.height
projectDescription.frame = projectDescriptionFrame
}
func textViewDidChange(textView: UITextView) {
projectDescriptionFrame.size.height = projectDescription.contentSize.height
projectDescription.frame = projectDescriptionFrame
var contentSizeheight = CGFloat()
for (index, viewInScrollView) in scrollView.subviews.enumerate() {
if index > 1 {
contentSizeheight += viewInScrollView.frame.height
}
}
scrollView.contentSize.height = contentSizeheight
}
func updateScrollView(withView withView: UIView) {
scrollView.addSubview(withView)
scrollView.contentSize.height += withView.frame.height
}
func scrollViewContentHeight() -> CGFloat {
var height = CGFloat()
for (index, viewInScrollView) in scrollView.subviews.enumerate() {
if index > 1 {
height += viewInScrollView.frame.height
}
}
return height
}
How can I do this so that it will adjust the UITextView frame and update the UIScrollView.contentSize (Not of UITextView) so then the user can scroll through the whole view. And I would say I want a UI like when you make a new message in iOS Mail app. There you are able to scroll through the emails subject and the body of the message as one. Please help me with this. I have been stuck on this for days.
Okay I figured it out. Here is what I changed.
In the global properties.
var oldProjectDescriptionHeight: CGFloat!
var projectDescriptionHeight: CGFloat! {
willSet {
oldProjectDescriptionHeight = projectDescriptionHeight
}
}
And in the UITextViewDelegate textViewDidChange
func textViewDidChange(textView: UITextView) {
let fixedWidth = textView.frame.size.width
textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
let newSize = textView.sizeThatFits(CGSize(width: fixedWidth, height: CGFloat.max))
var newFrame = textView.frame
newFrame.size = CGSize(width: max(newSize.width, fixedWidth), height: newSize.height)
textView.frame = newFrame
projectDescriptionHeight = newFrame.height
scrollView.contentSize.height += projectDescriptionHeight - oldProjectDescriptionHeight
}

UIImageView is moving back after new UIView is created

Hi I am trying to make a game using swift but am currently very stuck. My game has two buttons that move a UIImageView left and right which works perfectly although when my NSTimer calls to my animateBalls function every 2.5 seconds the UIImageView that was moved goes back to its original position. How would I be able to still be able to create a New UIView every 2.5 seconds but keep the UIIMageViews current position? Thanks for any help that you can give me, Here is my code:
import UIKit
class ViewController: UIViewController {
let speed = 10.0
#IBOutlet weak var mainBar: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
_ = NSTimer.scheduledTimerWithTimeInterval(2.5, target: self, selector: Selector("animateBalls"), userInfo: nil, repeats: true)
}
#IBAction func moveRightIsTapped(sender: AnyObject) {
if(mainBar.frame.origin.x < -158.5)
{
let xPosition = mainBar.frame.origin.x + 38.5
let yPosition = mainBar.frame.origin.y
let height = mainBar.frame.height
let width = mainBar.frame.width
mainBar.frame = CGRectMake(xPosition, yPosition, width, height)
}
}
#IBAction func moveLeftIsTapped(sender: AnyObject) {
if(mainBar.frame.origin.x > -389.5)
{
let xPosition = mainBar.frame.origin.x - 38.5
let yPosition = mainBar.frame.origin.y
let height = mainBar.frame.height
let width = mainBar.frame.width
mainBar.frame = CGRectMake(xPosition, yPosition, width, height)
}
}
#IBAction func playButtonTapped(sender: AnyObject) {
animateBalls()
}
func animateBalls() {
randomNum = Int(arc4random_uniform(1))
if(randomNum == 0)
{
let ball1 = UIView()
ball1.frame = CGRect(x: 121, y: -20, width: 20, height: 20)
ball1.layer.cornerRadius = 10
ball1.clipsToBounds = true
ball1.backgroundColor = UIColor.purpleColor()
self.view.addSubview(ball1)
UIView.animateWithDuration(speed, animations:{ ball1.frame = CGRect(x: 121, y: 705, width: ball1.frame.width, height: ball1.frame.height) })
}
if(randomNum == 1)
{
let ball2 = UIView()
ball2.frame = CGRect(x: 159, y: -20, width: 20, height: 20)
ball2.layer.cornerRadius = 10
ball2.clipsToBounds = true
ball2.backgroundColor = UIColor.greenColor()
self.view.addSubview(ball2)
UIView.animateWithDuration(speed, animations:{ ball2.frame = CGRect(x: 159, y: 705, width: ball2.frame.width, height: ball2.frame.height) })
}

Resources