Saturday, 30 January 2021

Swift - Doing Pinch gesture recognition

 Steps:

1. Create an instance to the pinch gesture class

2. Create a function that performs action

3. Add the gesture recogniser to the created instance

self.imageViews.addGestureRecognizer(UIPinchGestureRecognizer(target: self, action: #selector(self.pinchAction(gesture:)))) 

//Creating a function for pinch act

    @objc func pinchAction(gesture:UIPinchGestureRecognizer)

    {

        if let ges = gesture.view

        {

            ges.transform = ges.transform.scaledBy(x: gesture.scale, y: gesture.scale)

            gesture.scale = 1

            

        }

        

    }


Swift - Doing Pan gesture recognition

 Steps: 

1. Create a instance to the pan gesture class

2. Create a function that performs gesture action

3. Add gesture recognition to the pan gesture instance


Example:


self.imageViews.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.tapAction(tap:))))

//Creating a function for pan action

    @objc func panAction(gesture:UIPanGestureRecognizer)

    {

        let translation = gesture.translation(in: view)

        

        guard let mazeedView = gesture.view else{

            return

        }

        mazeedView.center = CGPoint(x: mazeedView.center.x + translation.x, y: mazeedView.center.y + translation.y)

        

        gesture.setTranslation(.zero, in: view)

    }


Saturday, 7 November 2020

Swift Value Types - OOPS

 In swift value types can be done through structs. In structs the instance is storing the value only. the below code is going to explain the how it works

struct Student{

    var name:String

}

var gulshan = Student(name:"Gulshan")

var mazeed = gulshan

mazeed.name = "Mazeed"

print(mazeed.name)

//prints the value as Mazeed

print(gulshan.name)

//prints the value as Gulshan

Swift Reference Types - OOPS

 In reference type we can store the memory address location into the instance of the class file below are the example program to explain this


class Student

{

    var name:String?

}

var gulshan = Student(name:"Gulshan")

var mazeed = gulshan

mazeed.name = "Mazeed"

print(gulshan.name)

// Prints the name as Mazeed

print(mazeed.name)

//Prints the name as Mazeed



Tuesday, 3 November 2020

Composition in OOPS

 Composition in oops means creating instances/methods of one class in the other class 

Example:


In swift program 

creating a class using class keyword

class Fruits

{

    var a:String = "Apple"

    var b:String = "Orange"

    var c:String = "Banana"

}

//Now we are creating instance to the Fruits class in another class

class ViewController: UIViewController {


    override func viewDidLoad() {

        super.viewDidLoad()

            let fruit = Fruits()

            print("fruit.a")//prints Apple

}

Saturday, 31 October 2020

A Swift program on functions without parameters

 In this program we are creating a function without parameters


func behaviour()

    {

       print("Biting: Both venomous and non-venomous species of snakes have fangs that they use to attack prey and defend themselves from threats.")

    }

    func habits()

    {

        print("Snake Diet: All snakes are carnivores, but their specific diets typically vary with their species and habitats.\nCommon snake foods include:  Insects, Eggs, Other Snakes, Lizards, Rabbits")

    }

Swift program inheritance with polymorphism - overriding

 In this program inheritance is done by using method overriding


class Animal: NSObject {

    //Creating and storing variables

    var eyes:UInt8?

    var tongue:UInt8?

    var heart:UInt8?

    var tail:UInt8?

    func bodyParts()

    {

        print("No.of eyes = \(eyes!)\nTongue      = \(tongue!)\nHearts      = \(heart!)\nTails      = \(tail!)")

    }

}

class Tiger: Animal {

    

    //Creating and storing a variable

    var ears:UInt8?

    var legs:UInt8?

    var mouth:UInt8?

    //Overriding the function from parent calss

    override func bodyParts()//Polymorphism

    {

        print("No.of eyes = \(eyes!)\nMouth      = \(mouth!)\nTongue      = \(tongue!)\nHearts      = \(heart!)\nTails      = \(tail!)\nEars      = \(ears!)\nLegs     = \(legs!)")

    }

}

Swift Program using functions with parameters

 In this program we are going to calculate the student results using functions with parameters


func tenthResults(stuName: String, telugu: UInt8, hindi: UInt8, english: UInt8, maths: UInt8, science: UInt8, social: UInt8)

    {

        //Creating and storing the constants

        let passMarksTenth:UInt8 = 35

        let maxTotalTenth:UInt16 = 600

        print("\(stuName) Tenth Results")

        print("::::::::::::::::::::::::")


        //Calculating the total marks

        let totalMarks:UInt16 = UInt16(telugu) + UInt16(hindi) + UInt16(english) + UInt16(maths) + UInt16(science) + UInt16(social)

            

        //Calculating the percentage of marks

        let tenthPercentage:Float = (Float(totalMarks)*100)/Float(maxTotalTenth)

            

        //if condition is used the check the subject wise result

            

        if telugu >= passMarksTenth

        {

            print("\(stuName)  ==>  Telugu Marks ==> \(telugu) --- Passed")

        }else

        {

            print("\(stuName)  ==>  Telugu Marks ==> \(telugu) --- Failed")

        }

        if hindi >= passMarksTenth

        {

            print("\(stuName) ==>  Hindi Marks ==> \(hindi) --- Passed")

        }else

        {

            print("\(stuName) ==>  Hindi Marks ==> \(hindi) --- Failed")

        }

        if english >= passMarksTenth

        {

            print("\(stuName) ==>  English Marks ==> \(english) --- Passed")

        }else

        {

            print("\(stuName) ==>  English Marks ==> \(english) --- Failed")

        }

        if maths >= passMarksTenth

        {

            print("\(stuName) ==>  Maths Marks ==> \(maths) --- Passed")

        }else

        {

            print("\(stuName) ==>  Maths Marks ==> \(maths) --- Failed")

        }

        if science >= passMarksTenth

        {

            print("\(stuName) ==>  Science Marks ==> \(science) --- Passed")

        }else

        {

            print("\(stuName) ==>  Science Marks ==> \(science) --- Failed")

        }

        if social >= passMarksTenth

        {

            print("\(stuName) ==>  Social Marks ==> \(social) --- Passed")

        }else

        {

            print("\(stuName) ==>  Social Marks ==> \(social) --- Failed")

        }

            

        //if conditon to check the overall result of student

        if (telugu >= passMarksTenth && hindi >= passMarksTenth && english >= passMarksTenth && maths >= passMarksTenth && science >= passMarksTenth && social >= passMarksTenth)

        {

            print("Congratulations! \(stuName) you have paseed in the examination")

            print("Percentage obtained by you is: \(percentage)")

            percentage = tenthPercentage

            grades()

          

        }else

        {

            print("Sorry! \(stuName) you have failed in the examination")

        }

            print("\(stuName) total marks is: \(totalMarks)")

            

    }

A Swift program on Optionals with functional return type

 In this program we are giving optional to the return values of the function


//Creating a function for Btech first year result using default parameters with optionals return

    func BTechFYResult(engMarks: UInt8, CLangMarks: UInt8, maths1Marks: UInt8 = 95, maths2Marks:UInt8 = 85, phyMarks:UInt8 = 98, BEEMarks:UInt8 = 70, EDCMarks:UInt8 = 65, drawingMarks:UInt8 = 85)->(result:Bool, total:UInt16, percentage:Float)?

    {

        var isFirstYear:Bool = true

        //Calculating the first year total and percentage

        let firstYearTotal:UInt16 = UInt16(engMarks) + UInt16(CLangMarks) + UInt16(maths1Marks) + UInt16(maths2Marks) + UInt16(phyMarks) + UInt16(BEEMarks) + UInt16(EDCMarks) + UInt16(drawingMarks)

        let firstYearPercentage:Float = (Float(firstYearTotal)*100)/Float(maxTotal)

        //Condition to check student is passed or failed

        if ((engMarks >= passMarks) && (CLangMarks >= passMarks) && (maths1Marks >= passMarks) && (maths2Marks >= passMarks) && (phyMarks >= passMarks) && (BEEMarks >= passMarks) && (EDCMarks >= passMarks) && (drawingMarks >= passMarks))

        {

            isFirstYear = true

        }else

        {

            isFirstYear = false

        }

        //Creating and storing the values in tuple

        var BTechFYResultSummary:(result:Bool, total:UInt16, percentage:Float) = (isFirstYear, firstYearTotal, firstYearPercentage)

        return BTechFYResultSummary

    }


//Creating a function for student results

    func shahinaResults()

    {

        //Creating and storing a variable

        var name = "SHAIK SHAHINA" // Type inference with string

        var shahinaResults = BTechFYResult(engMarks: 85, CLangMarks: 92 , maths1Marks: 86, maths2Marks: 66)

        //Unwrapping the optional return values and comparing

        if (((shahinaResults!.total) != nil&& ((shahinaResults!.percentage) != nil) )

        {

            //Condtion to check results

            if (shahinaResults!.result == true)

            {

                print("Conratulations \(name) you passed in the examination")

                print("Total Marks = \(shahinaResults!.total)\nPercentage = \(shahinaResults!.percentage)")

                percentage = shahinaResults!.percentage

                grades()

            }else

            {

                print("Sorry \(name) you have failed in the examination")

            }

        }

    }

A Swift Program on switch case

 In this program we are checking the student grades using switch case


//Creating and storing a variable

    var percentage:Float = 100

    //Creating a function to calculate student grades

    func grades()

    {

        switch percentage //switch case to check grades

        {

            case 70...100:

                print("Congratulations you got distinction")

            case 60..<70:

                print("Congratulations you got First Class")

            case 50..<60:

                print("Congratulations you got Second Class")

            case 40..<50:

                print("Congratulations you got Compartmental Class")

            default:

                print("Failed in exam")

            

        }


    }

A Swift program for to print Multiplication tables using loops

 In this program we print multiplication table using loops


Using For in loop, For in stride loop, While Loop, Repeat while loop


// Tables to avoid 1, 2, 4,7,9, 13, 15, 17, 21, 46, 49, 75, 98, 123, 146, 217, 413, 439, 467, 500

    //Multiples to avoid 1, 2, 3, 4, 7, 15, 23, 45, 54, 73, 86, 91, 92, 93, 95, 97

    //Creating a function

    func forLoops()

    {

        //Creating a for in loop for multiplication tables upto 500

        for i in 1...500

        {

            

            for j in 1...100

            {

                //condtion to skip execution at i values

                if(i == 1 || i == 2 || i == 4 || i == 7 || i == 9 || i == 13 || i == 15 || i == 17 || i == 21 || i == 46 || i == 49 || i == 75 || i == 98 || i == 123 || i == 146 || i == 217 || i == 413 || i == 439 || i == 467 || i == 500)

                {

                    continue

                }

               //Condition to skip execution at j values

                if(j == 1 || j == 2 || j == 3 || j == 4 || j == 7 || j == 15 || j == 23 || j == 45 || j == 54 || j == 73 || j == 86 || j == 91 || j == 92 || j == 93 || j == 95 || j == 97)

                {

                    continue

                }

            //Printing the multiplication tables

            print("\(i) * \(j) <<---->> \(i*j)")

            }

        }

    }

    //Creating a function

    func forStrideLoops()

    {

        //for in stride loops for multiplication tables upto 500

        for i in stride(from: 1, to: 500, by: 1)

        {

            for j in stride(from: 1, to: 100, by: 1)

            {

                //condtion to skip execution at i values

                if(i == 1 || i == 2 || i == 4 || i == 7 || i == 9 || i == 13 || i == 15 || i == 17 || i == 21 || i == 46 || i == 49 || i == 75 || i == 4 || i == 98 || i == 123 || i == 146 || i == 217 || i == 413 || i == 439 || i == 467 || i == 500)

                 {

                     continue

                 }

                //Condition to skip execution at j values

                 if(j == 1 || j == 2 || j == 3 || j == 4 || j == 7 || j == 15 || j == 23 || j == 45 || j == 54 || j == 73 || j == 86 || j == 91 || j == 92 || j == 93 || j == 95 || j == 97)

                 {

                     continue

                 }

            //Printing the multiplication tables

            print("\(i) * \(j) = \(i*j)")

            }

        }

    }

    //Creating a function

    func whileLopps()

    {

        //Creating and storing a variable

        var i:UInt16 = 1

        //Creating a while loop for multiplication tables up to 500

        while(i < 501)

        {

            for j in 1...100

            {

                //condtion to skip execution at i values

                if(i == 1 || i == 2 || i == 4 || i == 7 || i == 9 || i == 13 || i == 15 || i == 17 || i == 21 || i == 46 || i == 49 || i == 75 || i == 4 || i == 98 || i == 123 || i == 146 || i == 217 || i == 413 || i == 439 || i == 467 || i == 500)

                 {

                     continue

                 }

                //Condition to skip execution at j values

                 if(j == 1 || j == 2 || j == 3 || j == 4 || j == 7 || j == 15 || j == 23 || j == 45 || j == 54 || j == 73 || j == 86 || j == 91 || j == 92 || j == 93 || j == 95 || j == 97)

                 {

                     continue

                 }

                //Printing the multiplication tables

                print("\(i) multiples of \(j) = \(i*UInt16(j))")

               

            }

            i += 1//Increasing the i value by 1

            

        }

    }

    //Creating a function

    func repeatWhileLopps()

    {

        //Creating and storing a variable

        var i:UInt16 = 1

        //Creating a repeat while loop for multiplication tables up to 500

        repeat

        {

           

            for j in 1...100

            {

                //condtion to skip execution at i values

                if(i == 1 || i == 2 || i == 4 || i == 7 || i == 9 || i == 13 || i == 15 || i == 17 || i == 21 || i == 46 || i == 49 || i == 75 || i == 4 || i == 98 || i == 123 || i == 146 || i == 217 || i == 413 || i == 439 || i == 467 || i == 500)

                {

                     continue

                }

                

                //Condition to skip execution at j values

                 if(j == 1 || j == 2 || j == 3 || j == 4 || j == 7 || j == 15 || j == 23 || j == 45 || j == 54 || j == 73 || j == 86 || j == 91 || j == 92 || j == 93 || j == 95 || j == 97)

                 {

                     continue

                 }

                //Printing the multiplication tables

                print("\(i) times of \(j) = \(i*UInt16(j))")

            }

            i += 1//Increasing the i value by 1

        }while(i < 501)

    }

}