2016-01-10 48 views
0

在我的Swift應用程序中,我想將後置攝像頭指向對象,然後單擊按鈕。在我的視圖控制器中,我試圖拿起編程放置按鈕的UIButton按鈕,放置在cameraOverlayView上。UIButton不會在ImageView中調用處理程序函數

我不需要拍照 - 我只是用相機指向物體,然後點擊按鈕。

編譯爲iPhone。我似乎要麼攝像頭工作,要麼按鈕,但不能同時進行。 imagePicker坐在按鈕上並隱藏它。任何人都可以建議如何讓按鈕和imagePicker一起工作?提前致謝。

import UIKit 
import MobileCoreServices 

class FirstViewController: UIViewController { 

@IBOutlet weak var imageView: UIImageView! 
let imagePicker: UIImagePickerController! = UIImagePickerController() 

func noCamera() { 
    let alertVC = UIAlertController(
     title: "No Camera", 
     message: "Sorry, this device has no camera", 
     preferredStyle: .Alert) 
    let okAction = UIAlertAction(
     title: "OK", 
     style:.Default, 
     handler: nil) 
    alertVC.addAction(okAction) 
    presentViewController(
     alertVC, 
     animated: true, 
     completion: nil) 
} 

// THIS IS THE FUNCTION I'M TRYING TO CALL 
func buttonAction(sender: UIButton!) { 
    if sender.tag == 1 { 
     print("Button tapped") 
     let alertVC = UIAlertController(
      title: "Button pressed", 
      message: "Button pressed", 
      preferredStyle: .Alert) 
     let okAction = UIAlertAction(
      title: "OK", 
      style:.Default, 
      handler: nil) 
     alertVC.addAction(okAction) 
     presentViewController(
      alertVC, 
      animated: true, 
      completion: nil) 
    } 
} 

@IBAction func useCamera(sender: UIButton) { // A SEPARATE STORYBOARD BUTTON IS USED TO CALL THIS INITIALLY 
    if (UIImagePickerController.isSourceTypeAvailable(.Camera)) { 
     if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil { 
      //Create camera overlay 
      let pickerFrame = CGRectMake(
       0, 
       UIApplication.sharedApplication().statusBarFrame.size.height, 
       imagePicker.view.bounds.width, 
       imagePicker.view.bounds.height - imagePicker.navigationBar.bounds.size.height - imagePicker.toolbar.bounds.size.height) 

      // Sights 
      let sightDiam: CGFloat = 50 // size of sights 
      let sightFrame = CGRectMake(
       pickerFrame.width/2 - sightDiam/2, 
       pickerFrame.height/2 - sightDiam/2, 
       sightDiam, 
       sightDiam) 
      UIGraphicsBeginImageContext(pickerFrame.size) 
      let context = UIGraphicsGetCurrentContext() 
      CGContextSaveGState(context) 
      CGContextSetLineWidth(context, 2) // linewidth 
      CGContextSetStrokeColorWithColor(context, UIColor.yellowColor().CGColor) // colour 
      // Outer circle 
      CGContextStrokeEllipseInRect(context, sightFrame) 
      // Inner dot 
      CGContextStrokeEllipseInRect(context, CGRectMake(sightFrame.minX + sightFrame.width/2-1,sightFrame.minY + sightFrame.height/2-1,2,2)) 

      // Top tick 
      CGContextMoveToPoint(context, sightFrame.minX + sightFrame.width/2, sightFrame.minY + 7) 
      CGContextAddLineToPoint(context, sightFrame.minX + sightFrame.width/2, sightFrame.minY - 7) 
      // Bottom tick 
      CGContextMoveToPoint(context, sightFrame.origin.x + sightFrame.width/2, sightFrame.minY + sightFrame.size.height+7) 
      CGContextAddLineToPoint(context, sightFrame.origin.x + sightFrame.width/2, sightFrame.minY + sightFrame.size.height-7) 
      // Left tick 
      CGContextMoveToPoint(context, sightFrame.minX-7, sightFrame.minY + sightFrame.height/2) 
      CGContextAddLineToPoint(context, sightFrame.minX+7, sightFrame.minY + sightFrame.height/2) 
      // Right tick 
      CGContextMoveToPoint(context, sightFrame.minX + sightFrame.width-7, sightFrame.minY + sightFrame.height/2) 
      CGContextAddLineToPoint(context, sightFrame.minX + sightFrame.width+7, sightFrame.minY + sightFrame.height/2) 
      // Draw 
      CGContextStrokePath(context) 
      CGContextRestoreGState(context) 

      let overlayImage = UIGraphicsGetImageFromCurrentImageContext() 
      UIGraphicsEndImageContext(); 
      let overlayView = UIImageView(frame: pickerFrame) 
      overlayView.image = overlayImage 
      let screenSize = UIScreen.mainScreen().bounds.size 
      let aspectRatio:CGFloat = 4.0/3.0 
      let scale = screenSize.height/screenSize.width * aspectRatio 

      imagePicker.allowsEditing = false 
      imagePicker.sourceType = .Camera 
      imagePicker.cameraCaptureMode = .Photo 
      imagePicker.modalPresentationStyle = .FullScreen 
      imagePicker.showsCameraControls = false // keep off 
      imagePicker.cameraOverlayView = overlayView 
      imagePicker.cameraViewTransform = CGAffineTransformMakeScale(scale, scale); 
      overlayView.userInteractionEnabled = true 

      // Add Button (programatically) 
      let buttonBorder: CGFloat = 30 // size of button 
      let buttonHeight: CGFloat = 50 // height of button 
      var button: UIButton = UIButton(type: UIButtonType.System) as UIButton 
      button.frame = CGRectMake(
       buttonBorder, 
       screenSize.height - buttonBorder - buttonHeight, 
       screenSize.width - (buttonBorder * 2), 
       buttonHeight) 
      button.backgroundColor = UIColor.yellowColor() 
      button.setTitle("Aim at object", forState: UIControlState.Normal) 
      button.tag = 1 
      button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside) 
      button.userInteractionEnabled = true 
      overlayView.addSubview(button) 
      overlayView.bringSubviewToFront(button) 
      button.userInteractionEnabled = true 
      presentViewController(imagePicker, animated: false, 
       completion: {}) 
      // I WANTED THE BUTTON CLICK ABOVE TO CALL 'buttonAction' 
      // BUT THE BUTTON NEVER GETS ACTIVATED - WHY NOT? 

     } else { 
      noCamera() // no rear camera 
     } 
    } else { 
     noCamera() // no camera 
    } 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 
} 
} 

有很好的建議已經在後 - UIButton not calling event in imageView但這並沒有幫助我。由於

+0

你沒有提到'UIImagePickerController'' imagePicker'。在我看來你在添加按鈕之前呈現'imagePicker',所以這可能與它有關。 – tktsubota

+0

感謝TroyT - 你的意思是我應該把imagePicker定義(目前在代碼的頂部)放入useCamera函數中,但是首先定義按鈕? – Steve

+0

問題可能不是圖像視圖,它可能是圖像選擇器。嘗試創建一個新的簡單的應用程序,只有一個圖像視圖和一個按鈕,並看看是否可以點擊按鈕。 – tktsubota

回答

1

我想我已經破解它,併發布這個,如果它可以幫助別人。這是SO post是線索。

問題是UIImagePickerController上的cameraOverlay按鈕顯示在屏幕上,但沒有註冊觸摸。正如@Dia Kharrat在那篇文章中指出的那樣,「覆蓋UIView的框架不夠大。 UIView的clipsToBounds屬性默認設置爲NO,這意味着它的子視圖仍然可見,即使它們不在父框架之外......實質上,請確保覆蓋框的尺寸足夠大,或者覆蓋層的子視圖是定位在其範圍內「。我已將按鈕放置在覆蓋層外 - 但仍顯示出來。

我加了overlayView.clipsToBounds = true進行測試,按鈕在運行中消失,證明上面的註釋。這樣的解決方案是定義初始拾取幀作爲全屏幕大小,添加代碼:

let screenSize = UIScreen.mainScreen().bounds.size // line moved to top of code 
let pickerFrame = CGRectMake(0,0,screenSize.width,screenSize.height) // Full screen size 

的代碼的其餘部分上面保持不變。現在buttonAction被調用。感謝@TroyT和@ Fr4nc3sc0NL的幫助。

0

也許你不得不在func buttonAction(sender: UIButton!)

+0

該按鈕是以編程方式創建的,而不是以故事板創建的,因此不需要「@IBAction」。 – tktsubota

+0

非常感謝!但是 - 這並沒有解決它。無論如何,是不是鏈接到故事板的'@IBAction'?這是一個編程放置的按鈕。 – Steve

+0

啊好吧..你能按下按鈕什麼都沒有發生? (你看到新聞動畫)還是你不能按下按鈕? – Fr4nc3sc0NL

0

我看了看周圍其他SO職位,這種情況下,類似前面加上@IBAction,並有一些。 This post做了類似的事情,答案建議在imageView上設置userInteractionEnableddocumentation還指出,UIImageView覆蓋userInteractionEnabled財產爲false

所以相當多:

imageView.userInteractionEnabled = true 

你已經在評論中說,該按鈕上的看法上,所以這可能是那就是維持按鈕從接收觸摸事件的唯一的事情。

+0

謝謝@TroyT - 好吧,我試過了,上面的代碼是我運行它,仍然按鈕不連接。 'userInteractionEnabled'現在應用於按鈕和overlayView。我看到了你注意到的其他SO帖子和變通方法 - 但必須有一個簡單的解決方案。 – Steve

+0

@Steve嗯......沒關係。是的,必須有一個簡單的解決方案。我會繼續環顧四周,看看我能找到什麼。 – tktsubota

+0

我很感激你的幫助!我可以問你是否嘗試過運行這個?我不知道SO是否允許我分享項目以保存所有設置......? – Steve

相關問題