2

So I have the task of learning swift on the fly to make a very basic App. I need to add some "eye candy" so things need some animation, Can anyone help with a time constraint on learning and point me in a direction.

I would like to make the below code when inserting a image add a animation, any will do I just need to see how IOS is expecting me to add this in swift.

import UIKit
class TESTView: UIView {

    override func drawRect(rect: CGRect) {
        /*
         * Set the background image for the App
         */

        //Get the graphics context
        let context = UIGraphicsGetCurrentContext()
        //Get the size of the screen
        let entireScreen = UIScreen.mainScreen().bounds
        //Set the background image
        let backgroundImage = UIImage(named: "brush-strokes.jpg")
        //Fill screen with image
        backgroundImage?.drawInRect(entireScreen)

        /*
         * Set the icons for the menu
         */

        //Amount of menu items
        let menuIcons = 6
        //Get the icons in use
        let iconOne = UIImage(named: "UnRarX.png")
        //smaller size
        let newSize = CGSize(width: 50, height: 50)

        //Start image editing context to reduce size
        UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
        //Scale icon
        iconOne?.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height))
        //Get scaled icon
        let imageResized = UIGraphicsGetImageFromCurrentImageContext()
        //End image editing context
        UIGraphicsEndImageContext()

       /*
        * Lets try place the menu items in a circle
        */

        //TODO:Use 10% off all the edges of the screen size
        let radius = 100.0
        // add .0 to set as double for later
        let step = (2 * M_PI) / Double(menuIcons)
        // Divide by the number of icons
        var angle = 0.0 // Starting angle
        //Set 0 point for later
        var iconOnePos = CGPoint.zeroPoint
        //Loop icons No.
        for var i = 1; i <= menuIcons; ++i{
            //Calc the current X & Y vals using some maths
            var x = (entireScreen.width - 25) / 2 + CGFloat(radius * cos(angle)) // take off 25 for half the icon size
            var y = entireScreen.height / 2 + CGFloat(radius * sin(angle))
            //Create new point for the icon
            iconOnePos = CGPointMake(x, y)
            //Draw the image we resized
            imageResized?.drawAtPoint(iconOnePos)
            //Move the angle for the next calculation
            angle += step;
        }
    }


}
zc246
  • 1,514
  • 16
  • 28
Brad_ZA
  • 23
  • 5
  • What animation do you want to achieve? – zc246 Apr 11 '16 at 13:58
  • At this point just hitting the alpha prop will be super, I should be able to work out how to do more once I see how the interaction happens. This will all change the second I learn how it works :) 'good old corporate'. – Brad_ZA Apr 11 '16 at 14:03
  • 2
    You should probably _not_ try animation in `drawRect` – zc246 Apr 11 '16 at 14:07
  • Code Graphics (calls usually made in `drawRect`) is really only designed for static drawing. If you want animation, I'd recommend looking into Core Animation (`CALayer`, `CAShapeLayer`, `CABasicAnimation` etc). – Hamish Apr 11 '16 at 14:11
  • Probably worth sticking with basic `UIView` animations since the OP is just getting started – Aaron Brager Apr 11 '16 at 14:12
  • Sigh, "file->new". – Brad_ZA Apr 11 '16 at 14:12

0 Answers0