【问题标题】:UIButton to play when user touch drags their finger across the screen - Objective-C, IOS当用户触摸在屏幕上拖动手指时播放 UIButton - Objective-C,IOS
【发布时间】:2016-01-09 00:50:27
【问题描述】:

所以我有一个由 UIButtons 组成的键盘,每个 UIButtons 每个按钮都包含一个单独的声音。 当用户点击按钮时声音会播放,但我想知道当用户滑动在屏幕上拖动他们的触摸时,我如何才能播放它,就像钢琴滑奏一样,而不会将手指从屏幕上移开。

【问题讨论】:

    标签: objective-c uibutton piano


    【解决方案1】:

    您需要添加一个 PanGestureRecognizer。假设您将所有按钮都安排在 TableView 中。然后你需要从 UIPanGestureRecognizer 中识别出被触摸的 Cell。

    将 UIPanGestureRecognizer 添加到 Storyboard 中的 UITableView 或通过在 ViewDidLoad() 中编写以下代码

        UIPanGestureRecognizer *pRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panning:)];
        [self.tableView addGestureRecognizer:pRecognizer];
    

    然后在平移方法

        -(void)panning:(UIPanGestureRecognizer *)recognizer
        {
           CGPoint panLocation = [recognizer locationInView:self.tableView];
           NSIndexPath indexPath = [self.tableView indexPathForRowAtPoint:panLocation];
    
           //Get the cell from the table view
           UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
           **call TouchupInside event of your Key here**
        } 
    

    所以,现在您可以在用户平移您的按键时为所有按键播放声音。

    【讨论】:

    • 感谢您的回复。因此,当我为所有按钮创建一个表格视图时,它们都是单独的表格还是只是一个包含多个单元格的大表格视图?
    • 它只是一个具有更多单元格的 TableView。
    • @ D. Venkata Naresh 您能否指导我如何将 Pan Gesture 识别器添加到我的 UITableView?
    • 首先将平移手势添加到您的表格视图中。然后为该 PanGesture 创建一个 IBAction。通过使用我的答案中的陈述来查找哪个单元格被录音。然后手动调用该单元格的 TouchupInside 操作。
    • @D. Venkata Naresh 谢谢。如何将平移手势添加到我的 UITable 视图?抱歉,我还没有体验过使用 Pan Gesture。
    猜你喜欢
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    相关资源
    最近更新 更多