创建屏幕被点击的东西?迅速

时间:2016-05-21 05:26:08

标签: swift

我的基本想法是,我希望用户点击UIView中的某个位置,而不是屏幕上的任何位置,然后转到另一个页面来创建播放音频文件的文本标签或按钮的内容。

  1. 点按屏幕
  2. 创造一些东西
  3. 创建的东西出现在您点击的UIView中
  4. 即使是一些让我指向正确方向的伪代码也会很感激。

3 个答案:

答案 0 :(得分:1)

在ViewController上放置<?php $data = array( 'filename' => $_FILES["myfile"]["name"], 'checklist_item_id' => 2, // temporary checklist_item_id 'date_uploaded' => date('Y-m-d h:i:s'), 'uploaded_by' => $this->ion_auth->get_user_id() ); $result = $this->doc_item->insert($data); //return $result; move_uploaded_file($_FILES["myfile"]["tmp_name"], $output_dir.$fileName); $ret[]= $fileName; //echo json_encode($data['filename']); echo json_encode($ret); ?> 并将其TapGestureRecognizer操作连接到您的函数。由于手势识别器归ViewController所有,因此您需要检测屏幕上的分接点是否在目标视图中。

让您入门的代码段:
viewTapped是ViewController的视图 self.view是ViewController中的目标视图。

self.targetView

答案 1 :(得分:1)

我们从包含UIView的以下UILabel开始:

class Controller1: UIViewController {

    @IBOutlet weak var discCoverView: UIView!
    @IBOutlet weak var listenToMeLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        let discoCoverViewGesture = UITapGestureRecognizer(target:self, action:#selector(Controller1.discoCoverViewTap(_:)))
        self.discCoverView.addGestureRecognizer(discoCoverViewGesture)
    }   

    func discoCoverViewTap(recognizer: UITapGestureRecognizer) {
       // detect the location tapped by user
       let tapLocation = tapGestureRecognizer.locationInView(self.discCoverView)
       if self.listenToMeLabel.frame.contains(tapLocation) {
           // user tap the listenToMeLabel
           let storyboard = UIStoryboard(name: "Main", bundle: nil)
           let controller2 = storyboard.instantiateViewControllerWithIdentifier("controller2") as Controller2
           controller2.songTitle = self.listenToMeLabel.text
           self.presentViewController(controller2, animated: true, completion: nil)
       }
    }
}

Controller2可以是:

import AVFoundation
class Controller2: UIViewController {

      @IBOutlet weak var listenToMeBtn: UIButton!
      var songTitle: String! = "no title"

      override func viewDidAppear(animated: Bool) {
          super.viewDidAppear(animated)
          listenToMeBtn.setTitle(songTitle, forState: UIControlState.Normal)
          listenToMeBtn.addTarget(self, action: #selector(Controller2.listenToMeBtnTap(_:)), forControlEvents: UIControlEvents.TouchUpInside)
          listenToMeBtn.setNeedsLayout()
      }

      func listenToMeBtnTap(sender:UIButton) {
          print ("click \(sender.titleForState(UIControlState.Normal))")
          let url = NSBundle.mainBundle().URLForResource(songTitle, withExtension: "mp3")
          if (url == nil) {
             print("Could not find song file: \(songTitle)")
             return
          }
          do { backgroundMusicPlayer = try AVAudioPlayer(contentsOfURL: url!, fileTypeHint: nil) }
          catch let error as NSError { print(error.description) }

          if let player = backgroundMusicPlayer {
             player.volume = 0.7
             player.prepareToPlay()
             player.play()  
          }
      }
}

答案 2 :(得分:1)

最简单的方法:

class ViewController: UIViewController { 
     override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
    for touch in touches {
        let location = touch.locationInView(self.view)
        let newView = UIView(frame: CGRectMake(location.x, location.y, 100,100)
        newView.backgroundColor = UIColor.redColor()
        self.view.addSubview(newView)
    }
}