7

我正在创建一个相机应用程序,swift并且我有一个UIButton. 我想提出两个选项:当用户单击按钮时 - 它会拍照,当用户将手指放在按钮上时 - 它会录制电影,直到用户释放按钮。

我有录制和拍照的功能,现在我需要区分用户对按钮的操作。

此按钮的可用操作包括:

在此处输入图像描述

我试图开始录制touch down并停止录制touch up inside,但是我不知道我应该把负责拍照的代码放在哪里。如果我也把它放进去,touch down那么当用户开始录制电影时 - 也会拍照,我想避免它。

4

4 回答 4

6

点击和长按的手势识别器可以很好地相互配合以将其短路(点击延迟触发,直到确定它不是长按)。

    class ViewController: UIViewController{

        @IBOutlet weak var button: UIButton!
        override func viewDidLoad() {
            super.viewDidLoad()
            button.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tap)))
            let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPress))
            longPressGestureRecognizer.minimumPressDuration = 1
            button.addGestureRecognizer(longPressGestureRecognizer)
        }

        @objc private func tap(tapGestureRecognizer: UITapGestureRecognizer) {
            print("tap")
        }
        @objc private func longPress (longPressGestureRecognizer: UILongPressGestureRecognizer) {
            if longPressGestureRecognizer.state == .began {
                print("long press began")
            }

        }
    }
于 2016-11-04T19:31:00.880 回答
3

您可以使用UILongPressGestureRecognizer来录制视频,使用@IBAction - Touch Up Inside来拍照功能。

第 1 步:在情节提要中,创建一个UIButton并将UILongPressGestureRecognizer从 Object libray 拖到此按钮中

第 2 步:在ViewController.swift中,我们有以下代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var label: UILabel!
    @IBOutlet weak var button: UIButton!

    @IBAction func takePhoto(_ sender: AnyObject) {
        label.text = "Take photo"
    }

    @IBAction func recordVideo(_ sender: AnyObject) {
        label.text = "Record video"
    }
}

第 3 步:打开助手编辑器并连接这些@IBOutlet@IBAction

就是这样!

于 2016-11-05T01:09:44.887 回答
3

只需拍摄一张照片,您只需将按钮连接到带有 Touch Up Inside 的 IBAction。

要拍摄视频,您需要先将按钮连接到 IBOutlet,然后向其添加 UILongPressGestureRecognizer 以检测用户何时长按按钮。

@IBOutlet weak var button: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    let longPressGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longPress(gesture:)))
    longPressGesture.minimumPressDuration = 1 // Minimum duration to trigger the action
    self.button.addGestureRecognizer(longPressGesture)
}

func longPress(gesture: UILongPressGestureRecognizer) {
    if gesture.state == .began {
        print("Began")
        // Video starts recording
    } else if gesture.state == .ended {
        print("End")
        // Video stops recording
    }
}

@IBAction func takePhoto(_ sender: UIButton) {
    // Take photo
}
于 2016-11-06T09:21:16.583 回答
0

您可以使用以下代码在长按时提供按钮操作:

我使用手势来检测按钮的日志按下,它对我有用。

Objective-C

UILongPressGestureRecognizer *lPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(TakePhotoLongPressAction:)];
[self.btnPhoto addGestureRecognizer:lPress];
[lPress release];


- (void)TakePhotoLongPressAction:(UILongPressGestureRecognizer*)gevent
{
    if ( gevent.state == UIGestureRecognizerStateEnded ) {
         NSLog(@"Button Long Press");
    }
}

迅速

// add guesture recognizer
        let lPress = UILongPressGestureRecognizer(target: self, action: #selector(takePhotoLongPressAction(_:)))
        self.button.addGestureRecognizer(lPress)



func takePhotoLongPressAction(gevent: UILongPressGestureRecognizer) {
        if gevent.state == UIGestureRecognizerState.Began {
            print("Long Press button")
        }
    }

请检查并让我知道是否需要任何东西。

于 2016-11-11T13:05:42.503 回答