Tag: 快速

Swift如何在点击button时更新AVPlayer中的videourl?

我有一个IOS应用程序中的video播放器,我想点击一个button时更新video,但我不知道如何pipe理这个。 (注意:这不是队列中的video列表) 以下是添加AVPlayer的代码: let url = NSURL.fileURLWithPath(path) let player = AVPlayer(URL: url) let playerViewController = AVPlayerViewController() playerViewController.player = player .. .. self.ViewForVideo.addSubview(playerViewController.view) self.addChildViewController(playerViewController) player.play()

从HealthKit获取昨天的步骤

我正在构build一个个人使用的应用程序,而且我目前正在坚持如何准确地从healthkit得到昨天的步骤。 然后从那里,把它放到一个variables(应该很容易,我知道)。 我有一个HealthKitManager类,从视图内调用该函数,然后将其附加到来自同一视图的variables。 我已经search了大部分健康问题的资料,并且找回数据,但是我不认为这是准确的数据。 我昨天的电话数据是1442步,但是它返回了2665步。 最重要的是,当我试图把数据是一个variables,打印出0。 HealthKitManagerClass import Foundation import HealthKit class HealthKitManager { let storage = HKHealthStore() init() { checkAuthorization() } func checkAuthorization() -> Bool { // Default to assuming that we're authorized var isEnabled = true // Do we have access to HealthKit on this device? if HKHealthStore.isHealthDataAvailable() { // We have to […]

NSManagedObject在Swift中实现协议的EXC_BAD_ACCESS错误

我有以下两种方法: func isAuthenticated() -> Bool { var currentUser: CurrentUser? = self.getCurrentUser() if currentUser == nil { return false } self.token = getUserToken(currentUser!.username) if self.token == nil { return false } if !tokenIsValidForUser(self.token!, user: currentUser!) { return false } return true } func tokenIsValidForUser(token: AuthenticationToken, user: UserObject) -> Bool { if token.username != user.username { return […]

以协议types为关键字的Swift Dictionary

这里简短的问题: 我得到一个协议protocol SCResourceModel {..}和一个字典,它应该使用这个协议types作为关键字: [SCResourceModel : String] 。 这显然不起作用,因为字典中的关键字必须符合协议Hashable 。 使我的SCResourceModel从SCResourceModelinheritance或尝试像这样[protocol<SCResourceModel, Hashable> : String]显然不能正常工作,因为Hashable或Equatable只能用作通用约束,而不是作为types本身。 我观看了WWDC 2015,并且在Swift 2.0中,可以将约束添加到如下协议: protocol SCResourceModel where Self: Hashable {..}直接解决了这个问题(非常好)。 无论如何,我的问题是:我可以做一些类似的当前Swift 1.2版本,并以某种方式使用此协议作为字典的关键? 或者任何人都可以提出一个很好的解决方法或其他我可能忽略的东西? 我目前看到的Swift 1.2的唯一解决scheme是将协议转换为inheritance自NSObject的类,并且必须在我的API中进一步使用。 谢谢你的帮助!

animationCALayer背景颜色和更新模型值

我想为我的UIView(在tintColorDidChange )中的一个子图层的backgroundColor改变animation。 我需要从图层的当前背景颜色animation多次到新的色调颜色(每次不同的色调),所以需要更新backgroundColor的模型值(我不能在animation上使用removedOnCompletion = false )。 使用CABasicAnimation我有颜色更改animation工作正常,如果我不更新模型值(但当然animation完成后颜色重置)。 当我尝试更新模型值时,颜色变化立即发生,animation丢失。 我试图禁用隐式animation并使用CATransation更新模型值,但animation仍然丢失。 我如何更新backgroundColor模型的值,并保持我的淡出animation工作? override func tintColorDidChange() { super.tintColorDidChange() let colourAnim = CABasicAnimation(keyPath: "backgroundColor") colourAnim.toValue = self.tintColor.CGColor colourAnim.duration = 1.0 self.spinnerLayer?.addAnimation(colourAnim, forKey: "colourAnimation") CATransaction.begin() CATransaction.setDisableActions(true) self.spinnerLayer?.backgroundColor = self.tintColor.CGColor CATransaction.commit() }

iOS将电话号码转换为国际格式

在我的iOS应用程序中,我必须将电话号码(取自联系人)转换为国际格式(使用extern库自动发送短信)。 我看到了libPhoneNumber,但问题是我们必须input国家代码,并且应用程序必须在所有(几乎)国家工作,所以我不知道用户的国家是什么。 以下是图书馆的工作原理: let phoneUtil = NBPhoneNumberUtil() let phoneNumberConverted = try phoneUtil.parse("0665268242", defaultRegion: "FR") // So I don't know the country of the user here print(try? phoneUtil.format(phoneNumberConverted, numberFormat: NBEPhoneNumberFormat.INTERNATIONAL))

如何使用getData()在Swift中绘制带有ObjC PNChart库的LineChart?

我是一个新手程序员,最近开始学习Swift。 我正在Swift中创build一个项目,我的目的是在引入参数的同一个视图(使用由PNLineChart类控制的子视图)中绘制用户引入的信号函数。 事实上,我使用CocoaPods安装PNChart库,使用Objective C头文件的桥接头。 事情是,在ObjC中使用类和方法的方式是将它们“翻译”成Swift语法。 我发现PNChart有一个由作者制作的Swift实现,但是不能被Pods使用(如果有人知道如何将它们放到我的项目中,它也会有帮助)。 那么,作者在wiki中给出的数组图的ObjC代码如下所示: #import "PNChart.h" //For Line Chart PNLineChart * lineChart = [[PNLineChart alloc] initWithFrame:CGRectMake(0, 135.0, SCREEN_WIDTH, 200.0)]; [lineChart setXLabels:@[@"SEP 1",@"SEP 2",@"SEP 3",@"SEP 4",@"SEP 5"]]; // Line Chart No.1 NSArray * data01Array = @[@60.1, @160.1, @126.4, @262.2, @186.2]; PNLineChartData *data01 = [PNLineChartData new]; data01.color = PNFreshGreen; data01.itemCount = lineChart.xLabels.count; data01.getData = […]

Swift通用类作为委托

在swift中使用generics类作为委托时遇到了一个问题。 例如我试图使用通用的NSFetchedResultsDelegate定义为: class FetchedTableController<T:NSManagedObject> : NSFetchedResultsControllerDelegate 在课堂上,我设置了NSFetchedResultsController并将代理设置为“self”。 但由于某种原因,委托方法从未被调用过。 当我删除类的generics部分,委托按预期调用: class FetchedTableController : NSFetchedResultsControllerDelegate 有没有解决scheme使用generics类作为委托?

使用Swift以编程方式闪烁屏幕(在“屏幕截图”上)

为了从这里转换Objective C示例: 如何以编程方式刷屏? 我写了下面的代码: func blinkScreen(){ var wnd = UIApplication.sharedApplication().keyWindow; var v = UIView(frame: CGRectMake(0, 0, wnd!.frame.size.width, wnd!.frame.size.height)) wnd!.addSubview(v); v.backgroundColor = UIColor.whiteColor() UIView.beginAnimations(nil, context: nil) UIView.setAnimationDuration(1.0) v.alpha = 0.0; UIView.commitAnimations(); } 但我不知道应该在哪里添加UIView v移除代码(在animation结束时执行的某些事件,但是如何执行?)。 另外 – 我的转换是否正确?

当Autolay正在播放时,用UIDynamicAnimator进行animation处理

我有一个已经在InterfaceBuilder中configuration的ViewController为所有子视图使用AutoLayout。 布局工作正常。 我想用UIDynamicAnimator提供的很酷的重力效果让其中一个子视图反弹。 我不认为这将适用于AutoLayout,但我尝试过,它确实如此。 它工作得很好! 我的问题是 ,这是预期的行为? 是否会在某个地方引起问题? 苹果公司是否提供关于AutoLayout和UIDynamicAnimators相结合的指导? 这里是代码,对于任何感兴趣的人: var boundaryFrame = self.buttonBackgroundView.frame boundaryFrame = CGRect(x: 0, y: boundaryFrame.origin.y + (boundaryFrame.height + 1), width: self.view.frame.width, height: 2) self.animator = UIDynamicAnimator(referenceView: self.view) var gravity = UIGravityBehavior(items: [self.buttonBackgroundView]) gravity.magnitude = 0.5 var collision = UICollisionBehavior(items: [self.buttonBackgroundView]) var boundaryId: NSMutableString = NSMutableString(string: "bottomBoundary") let boundaryPath = UIBezierPath(rect: […]