小编Pip*_*ppo的帖子

擦洗时跳跃的 UISlider - 将 UISlider 与 AVPlayer 一起使用

我正在使用 AvPlayer 并尝试设置一个滑块以允许清理音频文件。我有一个问题,当它被选中时,滑块会跳到整个地方。然后它会回到原点位置一秒钟,然后再回到它被拖动到的位置。

你在 Gif 上看不到我的光标,但是平滑拉长的拖拽是我移动旋钮,然后快速鞭子是滑块行为不端。

在此处输入图片说明

我花了几个小时在谷歌上搜索和梳理 Stack Overflow,但无法弄清楚我在这里做错了什么,很多类似的问题都很老而且在 ObjC 中。

这是我认为导致问题的代码部分,它确实处理了滑块被移动的事件:我也尝试过没有 if 语句,但没有看到不同的结果。

@IBAction func horizontalSliderActioned(_ sender: Any) {

    horizontalSlider.isContinuous = true

    if self.horizontalSlider.isTouchInside {

        audioPlayer?.pause()
            let seconds : Int64 = Int64(horizontalSlider.value)
                let preferredTimeScale : Int32 = 1
                    let seekTime : CMTime = CMTimeMake(seconds, preferredTimeScale)
                        audioPlayerItem?.seek(to: seekTime)
                            audioPlayer?.play()

    } else {

        let duration : CMTime = (self.audioPlayer?.currentItem!.asset.duration)!
            let seconds : Float64 = CMTimeGetSeconds(duration)
                self.horizontalSlider.value = Float(seconds)
    }
}
Run Code Online (Sandbox Code Playgroud)

我将在下面包括我的整个班级以供参考。

import UIKit
import Parse
import AVFoundation
import AVKit


class PlayerViewController: …
Run Code Online (Sandbox Code Playgroud)

uislider ios avplayer swift

5
推荐指数
2
解决办法
2530
查看次数

迅速-在结构数组上使用.map

我有一个结构数组,我希望“分解”为较小的数组,可以根据需要调用它,或者至少可以弄清楚如何将一个文本值映射为所需的项。

结构:

struct CollectionStruct {
    var name : String
    var description : String
    var title : String
    var image : PFFile
    var id: String
}
Run Code Online (Sandbox Code Playgroud)

以及由struct构成的数组

var collectionArray = [CollectionStruct]()

var i = 0
for item in collectionArray {
    print(collectionArray[i].name)    
    i += 1  
}
Run Code Online (Sandbox Code Playgroud)

打印partArray [i] .name会得到以下结果:

pk00_pt01
pk00_pt02
pk00_pt03
pk01_pt01
pk01_pt02
pk01_pt03
pk01_pt04
pk01_pt05
pk01_pt06
pk01_pt07
pk01_pt08
Run Code Online (Sandbox Code Playgroud)

这只是一些测试值,但是这里可能有成千上万个条目,因此我只想按[i] .name的前4个字符过滤整个数组。 。地图?

arrays struct swift

4
推荐指数
1
解决办法
1467
查看次数

将参数传递给Swift中的选择器功能

我想将一个参数传递给一个从计时器调用为选择器的函数.具体来说,我可以在UI中更新内容.cell

那么我想要的是这样的:

timer = Timer.init(timeInterval: 1.0, target: self, selector: #selector(downloadTimer(cell: cell), userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)

功能

func downloadTimer(cell: InnerCollectionCell) {
    cell.progressBar.setProgress(downloadProgress, animated: true)
}
Run Code Online (Sandbox Code Playgroud)

虽然我可能会有点侄女假设这可以做到吗?

------编辑------

根据以下示例,但没有像往常一样从单元格获得预期结果

let innerCell: InnerCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifierInner, for: indexPath) as! InnerCollectionCell

timer = Timer.init(timeInterval: 1.0, target: self, selector: #selector(downloadTimer(_:)), userInfo: innerCell, repeats: true)


func downloadTimer(_ timer: Timer) {

    let cell = timer.userInfo

    cell. // no options as expected of a cell

}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果数据发送正确,我期待更多这样的选项:

在此输入图像描述

parameters timer selector ios swift

4
推荐指数
2
解决办法
2万
查看次数

collectionview scrollToItemAtIndexPath使应用程序崩溃

所以我在我的viewDidLoad函数中放置了以下代码,它使应用程序崩溃。任何想法为什么会发生这种情况或解决方法?

self.collectionView?.scrollToItem(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
Run Code Online (Sandbox Code Playgroud)

---------------编辑---------------添加了错误日志

2016-12-26 18:40:08.354 ParseStarterProject-Swift[73829:1561943] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'attempt to scroll to invalid index path: <NSIndexPath: 0x608000226400> {length = 2, path = 0 - 0}'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001066e7d4b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x000000010614921e objc_exception_throw + 48
    2   CoreFoundation                      0x00000001067512b5 +[NSException raise:format:] + 197
    3   UIKit                               0x0000000107fb0e44 -[UICollectionView scrollToItemAtIndexPath:atScrollPosition:animated:] + 224
    4   ParseStarterProject-Swift           0x00000001051a963c _TFC25ParseStarterProject_Swift18PackViewController13viewDidAppearfSbT_ + 700
    5   ParseStarterProject-Swift …
Run Code Online (Sandbox Code Playgroud)

xcode ios uicollectionview swift swift3

2
推荐指数
1
解决办法
3219
查看次数

复制之前锁定整个词典或部分词典(SyncRoot)

我在 Blazor 服务器应用程序的范围服务中有 2 个字典,我用它来管理多租户的状态。我注意到用户在不同线程上修改字典可能存在并发问题。

public Dictionary<string, GlobalDataModel> Global { get; } = new();
public Dictionary<string, Dictionary<long, LocalDataModel>> Local { get; } = new();
Run Code Online (Sandbox Code Playgroud)

我想我倾向于将它们保留为普通字典并在修改或迭代时锁定同步根。

如果我要将一个项目添加到包含类中的集合中,它会是:

if (Local.ContainsKey(tenantId) == false)
{
    lock (syncRoot)
    {
        Local.Add(tenantId, new Dictionary<long, LocalDataModel>());
    }
}
Run Code Online (Sandbox Code Playgroud)

或者:

lock (syncRoot)
{
    if (Local.ContainsKey(tenantId) == false)
    {
        {
            Local.Add(tenantId, new Dictionary<long, LocalDataModel>());
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

那么,如果我要将服务集合的不同部分从外部类复制到列表中,以便可以安全地迭代它,我是否只锁定在服务级别、字典级别或数据模型级别,或者它是依赖的?

假设我想要一个特定项目中的资源集合。可不可能是:

[Inject] private IDataAdaptor DataAdaptor { get; set; };
var resources;
    
lock (DataAdaptor)
{
    resources = DataAdaptor.Local[TenantId][ProjectId].Resources;
}
Run Code Online (Sandbox Code Playgroud)

或者:

lock (DataAdaptor.Local[TenantId][ProjectId].Resources)
{ …
Run Code Online (Sandbox Code Playgroud)

c# multithreading thread-safety syncroot blazor

2
推荐指数
1
解决办法
165
查看次数

UICollectionViewScrollPosition不起作用

我有一个collectionview,我试图以编程方式滚动.问题是,如果要滚动到的单元格在集合视图中可见,则不会将其滚动到中心.所以对于下面的图像,下面的单元格是项目1.它不会滚动到它,但它将滚动到项目1到项目2.

我一直在尝试使用UICollectionVieScrollPosition.CenterVertically,但这似乎不起作用.

self.collectionView?.scrollToItem(at: IndexPath(row: 1, section: 0), at: UICollectionViewScrollPosition.centeredVertically, animated: true)
Run Code Online (Sandbox Code Playgroud)

有没有办法绕过这个强制滚动对集合中心可见的单元格?

在此输入图像描述

ios uicollectionview swift

1
推荐指数
3
解决办法
4130
查看次数

AVPlayer-从本地URL播放

令人惊讶的是,尽管进行了很多搜索,但我仍无法找到答案。

我有一个已缓存的文件,我可以检索我认为是url的缓存目录和文件名吗?

这是获取文件然后播放的代码

let cachedFilePath = getCachedFilePath.result as! String
audioFileURL = cachedFilePath
self.audioPlayerItem = AVPlayerItem(url: NSURL(string: audioFileURL!) as! URL)
Run Code Online (Sandbox Code Playgroud)

这是如果我打印返回的内容audioFileURL

/ Users / Genie / Library / Developer / CoreSimulator / Devices / A2FB00CE-B018-4FDF-9635-35FD6678DF8D / data / Containers / Data / Application / E18B5E89-B973-4277-AA5C-1378C69D80CD / Library / Caches / Parse / PFFileCache / 7e4a8f655859 %20Name%20of%20the%20Wind%2017-92.mp3

播放器加载,但从不播放,它只是坐在那里旋转。所以我想知道我是否将URL正确传递给播放器?

我读过一个要使用的线程,file://但是没有用。

ios avplayer swift

1
推荐指数
1
解决办法
2199
查看次数

显示可重用单元格中的下载进度

我试图在我的collectionview单元格中显示下载进度.我当前正在使用具有单元实例的解析进度块并更新进度条.

}, progressBlock: { (percent) in
    self.mainQueue.addOperation {

    // set the downloadProgess var to update from cellForItemAt
    // downloadProgress = (Float(percent) / Float(100))

    if let downloadingCell = self.collectionView.cellForItem(at: self.indexPath) as? InnerCollectionCell {
        downloadingCell.progressBar.isHidden = false
        downloadingCell.contentView.bringSubview(toFront: downloadingCell.progressBar)
        downloadingCell.progressBar.setProgress(Float(percent) / Float(100), animated: true)
        downloadingCell.setNeedsDisplay()
        downloadingCell.setNeedsLayout()
        downloadingCell.isUserInteractionEnabled = false
        downloadingCell.spinner.isHidden = true
    }
}
})
Run Code Online (Sandbox Code Playgroud)

所以这个工作正常,我现在遇到的问题是,如果我离开这个视图控制器,然后回来看看下载是如何进行的,单元格的实例已被重用,并且没有任何所需的UI元素可见,但进度仍在滴答作响在背景中.

我可以想到重新显示UI元素的唯一地方是在cellForItemAt中.那么问题是进度不会更新,它只显示重新加载单元格时的值.

我如何重用进度块正在使用的单元格实例或干净地显示继续更新的ui元素?

uiprogressview ios parse-platform uicollectionview swift

1
推荐指数
1
解决办法
1028
查看次数

将十六进制颜色作为字符串转换为 Uint

我一直在寻找这个,但找不到有效或我理解的解决方案。

我在数据库“0x6c8c93”中有一个字符串,我想将其转换为 Uint,以便将其转换为颜色。

下面是我以前用来将 Uint 转换为颜色的函数。我刚刚将颜色图表中的十六进制值以这种格式 0x6c8c93 传递给它。但是现在我需要从数据库中提取一些值,所以我必须使用字符串。

class func UIColorFromHEX(hexValue: UInt) -> UIColor {
    return UIColor(
        red: CGFloat((hexValue & 0xFF0000) >> 16) / 255.0,
        green: CGFloat((hexValue & 0x00FF00) >> 8) / 255.0,
        blue: CGFloat(hexValue & 0x0000FF) / 255.0,
        alpha: CGFloat(1.0)
    )
}
Run Code Online (Sandbox Code Playgroud)

感谢帮助。

hex uicolor uint swift

0
推荐指数
1
解决办法
2336
查看次数