কন্ট্রোলারে ইউআইভিউ টাচ ইভেন্ট


98

এক্সকোড মেইনস্টোরবোর্ড থেকে সরবরাহ না করায় আমি কীভাবে ইউআইভিউ টাচবেগিন অ্যাকশন যুক্ত করতে পারি বা প্রোগ্রামটিমে অ্যাকশনটি স্পর্শ করতে পারি?


এটি হ'ল বোতামটির জন্য, ওপি
— ইউআইভিউ

শূন্যতে সেট UILongPressGestureRecognizerসহ একটি ব্যবহার করুন minimumPressDuration। এই উত্তর দেখুন। এর জন্য সাবক্লাসিং বা কোনও কিছুকে ওভাররাইড করার দরকার নেই।
— সুরগাচ

উত্তর:


151

আপনাকে কোডের মাধ্যমে এটি যুক্ত করতে হবে। এটা চেষ্টা কর:

    // 1.create UIView programmetically
    var myView = UIView(frame: CGRectMake(100, 100, 100, 100))
    // 2.add myView to UIView hierarchy
    self.view.addSubview(myView) 
    // 3. add action to myView
    let gesture = UITapGestureRecognizer(target: self, action: "someAction:")
    // or for swift 2 +
    let gestureSwift2AndHigher = UITapGestureRecognizer(target: self, action:  #selector (self.someAction (_:)))
    self.myView.addGestureRecognizer(gesture)

    func someAction(sender:UITapGestureRecognizer){     
       // do other task
    }

    // or for Swift 3
    func someAction(_ sender:UITapGestureRecognizer){     
       // do other task
    }

    // or for Swift 4
    @objc func someAction(_ sender:UITapGestureRecognizer){     
       // do other task
    }

    // update for Swift UI

    Text("Tap me!")
        .tapAction {
             print("Tapped!")
        }

4
আমি যখন করেছি আমি যখন ইউভিউতে ক্লিক করি তখন এটি আমাকে একটি ত্রুটি দিচ্ছে। আমার কোড: আসুন অঙ্গভঙ্গি = ইউআইটিএপজেষ্টারআর সনাক্তকারী (টার্গেট: সেলফইউইপেন্ডিংভিউ, অ্যাকশন: "টাচপেন্ডিং") সেলফইউইপেন্ডিংভিউ.এডডি জেচারআরকিউনাইজার (অঙ্গভঙ্গি) এবং পদ্ধতি: ফানক টাচপেন্ডিং (প্রেরক: যেকোন অবজেক্ট) {প্রিন্টলন ("পদ্ধতি >>>>>>> >>>>>>>>>> ""}
— ধাওয়াল শাহ

4
কেবল যুক্ত করুন: "টাচপেন্ডিং:" এ ফাংশনগুলিতে উত্তরগুলি দেখতে মজাদার টাচপেন্ডিংয়ের মতো দেখায় (প্রেরক: ইউআইটিপজেশারআরসিগনাইজার)
— রিজওয়ান শাইখ

4
আপনি কর্মে ':' নিখোঁজ রয়েছেন।
— মিকনাশ

হাই, আমি যখন কীভাবে ইউআইভিউ-র ক্লিকগুলি পেয়েছিলাম তখন তাদের আলাদা আলাদা আলাদাভাবে কাজ করতে পারলে কীভাবে আমি আলাদা করতে পারি?
— সি উইলিয়ামস

সবচেয়ে সহজ উপায় হ'ল ট্যাগ সম্পত্তি ব্যবহার করা এবং তারপরে
— ফাংশনটিতে

72

সুইফ্ট 4/5:

let gesture = UITapGestureRecognizer(target: self, action:  #selector(self.checkAction))
self.myView.addGestureRecognizer(gesture)

@objc func checkAction(sender : UITapGestureRecognizer) {
    // Do what you want
}

সুইফট 3:

let gesture = UITapGestureRecognizer(target: self, action:  #selector(self.checkAction(sender:)))
self.myView.addGestureRecognizer(gesture)

func checkAction(sender : UITapGestureRecognizer) {
    // Do what you want
}

4
প্রথম 2 টি রেখাটি ভিউডব্লাইডের মাধ্যমে বলা উচিত!
— ওলেকসান্ডার

25

সুইচ 3.x এর জন্য @ ক্র্যাশলোটের উত্তর আপডেট করা:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let currentPoint = touch.location(in: self)
        // do something with your currentPoint
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let currentPoint = touch.location(in: self)
        // do something with your currentPoint
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let currentPoint = touch.location(in: self)
        // do something with your currentPoint
    }
}

19

সুইচট 2.x এর জন্য @ চ্যাকেলের উত্তর আপডেট করা:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = touches.first {
        let currentPoint = touch.locationInView(self)
        // do something with your currentPoint
    }
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = touches.first {
        let currentPoint = touch.locationInView(self)
        // do something with your currentPoint
    }
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = touches.first {
        let currentPoint = touch.locationInView(self)
        // do something with your currentPoint
    }
}

8

UIViewএটি আপনার সাবক্লাসে রাখুন (আপনি যদি এই কার্যকারিতার জন্য একটি সাবলকাস তৈরি করেন তবে এটি সবচেয়ে সহজ)।

class YourView: UIView {

  //Define your initialisers here

  override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    if let touch = touches.first as? UITouch {
      let currentPoint = touch.locationInView(self)
      // do something with your currentPoint
    }
  }

  override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
    if let touch = touches.first as? UITouch {
      let currentPoint = touch.locationInView(self)
      // do something with your currentPoint
    }
  }

  override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
    if let touch = touches.first as? UITouch {
      let currentPoint = touch.locationInView(self)
      // do something with your currentPoint
    }
  }
}

@ চাকা, আমার পৃষ্ঠায় আমার 10 টিরও বেশি ইউভিউ রয়েছে এবং আমি কিছু উপ ইউআইভিউতে ক্রিয়া যুক্ত করতে চাই। তাহলে তার জন্য আমার কী পরিবর্তন করা উচিত?
— ধাওয়াল শাহ

এটি নির্ভর করে আপনি এটির জন্য কী ব্যবহার করতে চান। আপনি কোনটি UIViewচাপবেন তা বলতে চান বা আপনি প্রতিটিটিতে কয়েকটি টিপুন পরিচালনা করতে চান UIView?
— চকলে

আমি কেবল কিছু নির্দিষ্ট ইউআইভিউয়ের জন্য স্পর্শকাতর চাই।
— ধাওয়াল শাহ

8

দ্রুত 4 এর জন্য

@IBOutlet weak var someView: UIView!  
let gesture = UITapGestureRecognizer(target: self, action:  #selector (self.someAction (_:)))
self.someView.addGestureRecognizer(gesture)

@objc func someAction(_ sender:UITapGestureRecognizer){
    print("view was clicked")
}

6

স্টোরিবোর্ডে তৈরি করা ভিউগুলি থেকে আউটলেটগুলি তৈরি করুন।

@IBOutlet weak var redView: UIView!
@IBOutlet weak var orangeView: UIView!
@IBOutlet weak var greenView: UIView!   

টাচস বিগান পদ্ধতিটি ওভাররাইড করুন। 2 টি বিকল্প রয়েছে, প্রত্যেকে নির্ধারণ করতে পারবেন যে কোনটি তার জন্য ভাল।

  1. বিশেষ দৃশ্যে স্পর্শ সনাক্ত করুন।

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
         if let touch = touches.first {
            if touch.view == self.redView {
                tapOnredViewTapped()
            } else if touch.view == self.orangeView {
                orangeViewTapped()
            } else if touch.view == self.greenView {
                greenViewTapped()
            } else {
                return
            }
        }
    
    }
    
  2. বিশেষ দৃশ্যে স্পর্শ পয়েন্ট সনাক্ত করুন।

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first {
            let location = touch.location(in: view)
            if redView.frame.contains(location) {
                redViewTapped()
            } else if orangeView.frame.contains(location) {
                orangeViewTapped()
            } else if greenView.frame.contains(location) {
                greenViewTapped()
            }
        }
    
    }
    

শেষ অবধি, আপনাকে যে ফাংশনগুলি বলা হবে তা ঘোষণা করতে হবে, ব্যবহারকারী কোন ভিউ ক্লিক করেছে তার উপর নির্ভর করে।

func redViewTapped() {
    print("redViewTapped")
}

func orangeViewTapped() {
    print("orangeViewTapped")
}

func greenViewTapped() {
    print("greenViewTapped")
}

খুব ভাল দুর্দান্ত উদাহরণ আমাকে দেখানোর জন্য ধন্যবাদ !! আমরা এটি টাচ-এভেন্ট ব্যবহার করেও করতে পারি .. আমি কেবল দুটি উপায় বোতাম এবং অঙ্গভঙ্গি জানি .. ধন্যবাদ +1
— যোগেশ প্যাটেল

5

সুইফট 4.2:

@IBOutlet weak var viewLabel1: UIView!
@IBOutlet weak var viewLabel2: UIView!
  override func viewDidLoad() {
    super.viewDidLoad()

    let myView = UITapGestureRecognizer(target: self, action: #selector(someAction(_:)))
    self.viewLabel1.addGestureRecognizer(myView)
}

 @objc func someAction(_ sender:UITapGestureRecognizer){
   viewLabel2.isHidden = true
 }

4

আপনি এইভাবে ব্যবহার করতে পারেন: এক্সটেনশন তৈরি করুন

extension UIView {

    func addTapGesture(action : @escaping ()->Void ){
        let tap = MyTapGestureRecognizer(target: self , action: #selector(self.handleTap(_:)))
        tap.action = action
        tap.numberOfTapsRequired = 1

        self.addGestureRecognizer(tap)
        self.isUserInteractionEnabled = true

    }
    @objc func handleTap(_ sender: MyTapGestureRecognizer) {
        sender.action!()
    }
}

class MyTapGestureRecognizer: UITapGestureRecognizer {
    var action : (()->Void)? = nil
}

এবং এইভাবে ব্যবহার করুন:

@IBOutlet weak var testView: UIView!
testView.addTapGesture{
   // ...
}

2

উপরের উত্তরের জন্য কেবল একটি আপডেট:

আপনি যদি ক্লিক ইভেন্টে পরিবর্তনগুলি দেখতে চান, অর্থাত্ আপনার ইউআইভি রঙের রঙের পরিবর্তন যখনই ব্যবহারকারী ইউআইভিউতে ক্লিক করেন তবে নীচের মত পরিবর্তন করুন ...

class ClickableUIView: UIView {
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
            if let touch = touches.first {
                let currentPoint = touch.locationInView(self)
                // do something with your currentPoint
            }

            self.backgroundColor = UIColor.magentaColor()//Color when UIView is clicked.
        }

        override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
            if let touch = touches.first {
                let currentPoint = touch.locationInView(self)
                // do something with your currentPoint
            }

            self.backgroundColor = UIColor.magentaColor()//Color when UIView is clicked.
        }

        override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
            if let touch = touches.first {
                let currentPoint = touch.locationInView(self)
                // do something with your currentPoint
            }

            self.backgroundColor = UIColor.whiteColor()//Color when UIView is not clicked.

}//class closes here

এছাড়াও স্টোরিবোর্ড এবং ভিউকন্ট্রোলার থেকে এই ক্লাসটি কল করুন:

@IBOutlet weak var panVerificationUIView:ClickableUIView!

2

সুইট ৪.x এর জন্য @ স্টিভো.মিটের উত্তর আপডেট করা:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let currentPoint = touch.location(in: self.view)
        // do something with your currentPoint
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let currentPoint = touch.location(in: self.view)
        // do something with your currentPoint
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        let currentPoint = touch.location(in: self.view)
        // do something with your currentPoint
    }
}
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.