小编DCD*_*CDC的帖子

Swift - 彩色填充动画

我对ios动画相对较新,我相信我为UIView制作动画的方法有些不对劲.

我将从UI截图开始,更准确地描绘我的问题:

有一个tableView单元格,有两个标签和彩色圆圈 在此输入图像描述

每当我向单元格引入新的价值时,我想为这个最左边的灯泡制作动画,让它看起来像是充满了红色.

这是BadgeView的实现,它基本上是前面提到的最左边的圆圈

class BadgeView: UIView {

var coeff:CGFloat = 0.5

override func drawRect(rect: CGRect) {
    let topRect:CGRect = CGRectMake(0, 0, rect.size.width, rect.size.height*(1.0 - self.coeff))
    UIColor(red: 249.0/255.0, green: 163.0/255.0, blue: 123.0/255.0, alpha: 1.0).setFill()
    UIRectFill(topRect)

    let bottomRect:CGRect = CGRectMake(0, rect.size.height*(1-coeff), rect.size.width, rect.size.height*coeff)
    UIColor(red: 252.0/255.0, green: 95.0/255.0, blue: 95.0/255.0, alpha: 1.0).setFill()
    UIRectFill(bottomRect)
            self.layer.cornerRadius = self.frame.height/2.0
    self.layer.masksToBounds = true
   }
}
Run Code Online (Sandbox Code Playgroud)

这是我实现不均匀填充的方式 - 我引入了我在viewController中修改的系数.

在我的cellForRowAtIndexPath方法中,我尝试使用带回调的自定义按钮为此形状设置动画

let btn:MGSwipeButton = MGSwipeButton(title: "", icon: img, backgroundColor: nil, insets: ins, callback: {
        (sender: MGSwipeTableCell!) -> …
Run Code Online (Sandbox Code Playgroud)

core-animation uitableview ios swift

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

Firebase Swift - 如何创建子项并将其id添加到另一个ref属性?

如上所述这里,我想存放图书的对象在一个单独的参考价值和它的ID存储内books的财产User

Users:
user_id:121jhg12h12
  email: "john@doe.com"
  name: "John Doe"
  profile_pic_path: "https://...".
  language: "en"
  exp_points: 1284
  friends: [user_id]
  books: [[book_id, status, current_page, start_date, finish_date]]
  badges: [[badge_id, get_date]]

Books:
book_id: 3213jhg21
  title: "For whom the bell tolls"
  author: "Ernest Hemingway"
  language: "en"
  pages_count: 690
  ISBN: "21hjg1"
  year: 2007
Run Code Online (Sandbox Code Playgroud)

每当我在应用程序中添加一本书时

self.ref!.child("Books").childByAutoId().setValue(["title": arrayOfNames[0] as! NSString, "author": arrayOfNames[1] as! NSString , "pages_count":arrayOfNames[2] as! NSString])
Run Code Online (Sandbox Code Playgroud)

book对象是在Books ref中创建的,但我想立即将其id添加到User的用户书籍数组中.

可以用一些漂亮的方式完成,而不是查询书籍,检索它的ID并将其添加到数组中吗?

如果不是,查询刚刚创建的对象id的正确方法是什么?

也许我不应该使用AutoId模式并在应用程序中为自己创建每个对象的唯一ID?

ios firebase swift firebase-realtime-database swift3

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

Swift 3:Transfer Utility enumerateToAssignBlocks方法签名

官方AWS文档包含以下Objective-c代码

 AWSS3TransferUtility *transferUtility = [AWSS3TransferUtility defaultS3TransferUtility];
[transferUtility
 enumerateToAssignBlocksForUploadTask:^(AWSS3TransferUtilityUploadTask *uploadTask, __autoreleasing AWSS3TransferUtilityUploadProgressBlock *uploadProgressBlockReference, __autoreleasing AWSS3TransferUtilityUploadCompletionHandlerBlock *completionHandlerReference) {
     NSLog(@"%lu", (unsigned long)uploadTask.taskIdentifier);

     // Use `uploadTask.taskIdentifier` to determine what blocks to assign.

     *uploadProgressBlockReference = // Reassign your progress feedback block.
     *completionHandlerReference = // Reassign your completion handler.
 }
 downloadTask:^(AWSS3TransferUtilityDownloadTask *downloadTask, __autoreleasing AWSS3TransferUtilityDownloadProgressBlock *downloadProgressBlockReference, __autoreleasing AWSS3TransferUtilityDownloadCompletionHandlerBlock *completionHandlerReference) {
     NSLog(@"%lu", (unsigned long)downloadTask.taskIdentifier);

     // Use `downloadTask.taskIdentifier` to determine what blocks to assign.

     *downloadProgressBlockReference =  // Reassign your progress feedback block.
     *completionHandlerReference = // Reassign your completion handler. …
Run Code Online (Sandbox Code Playgroud)

amazon-s3 amazon-web-services ios swift swift3

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

iOS 8 Mapkit和位置 - 每天的请求数量是否有限制?

正如标题所说,苹果没有提供任何明确的答案.我不想使用谷歌API,因为它的请求有限制,我想知道iOS 8中的MapKit是否有任何?(到目前为止还没有这样的限制,但每次发布的iOS都可能会发生变化).

如果没有这样的限制,在iOS8版本中使用MapKit有什么缺点?Google Maps API是否会变得更有帮助?

提前致谢

geolocation mapkit ios swift

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

Swift - 将数据转换为十进制值

我通过BLE获取数据,我想将它们转换为十进制值,但我怀疑我做错了,因为值范围似乎不正确

来自BLE,我收到了 characteristic.value

24ffa400 6d08fcff fffffbff 0d001500 5eff

它是由IMU值构成的.

当我尝试转换前两个字节时,在这个例子24ff中是加速度计X轴值,我从范围1000-30000得到一个值(我向传感器引入了一些动态运动,看看值是如何变化的).

这一定是错的,因为文档说加速计的规模是±16G 有两个更重要的信息:

帧中的字节序列如下:[LSB,MSB]

值为16位,使用二进制补码

这是我如何将数据转换为十进制值:

class func getAccelerometerData(value: NSData) -> [String] {

    let dataFromSensor = dataToSignedBytes8(value)
    let bytes:[Int8] = [dataFromSensor[1], dataFromSensor[0]]

    let u16 = UnsafePointer<Int16>(bytes).memory
    return([twosComplement(u16)])
}

class func twosComplement(num:Int16) -> String {
    var numm:UInt16 = 0
    if num < 0 {
        let a = Int(UInt16.max) + Int(num) + 1
        numm = UInt16(a)
    }
    else { return String(num, radix:10) }
    return String(numm, radix:10)
}
Run Code Online (Sandbox Code Playgroud)

我想我应该从范围<-16:16>获得值而不是上面提到的巨大值,我的方法有什么问题?

提前致谢

编辑:缺少方法实现 …

hex ios core-bluetooth swift

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

如何按枚举值对对象进行排序?

我上了财产WorkoutDifficulty

enum Difficulty: String {
  case easy = "easy"
  case moderate = "moderate"
  case hard = "hard"
}

class Workout {
  var name: String?
  var difficulty: Difficulty?
  .
  .
  .
}
Run Code Online (Sandbox Code Playgroud)

我想通过难度属性对一系列训练进行排序.我知道我可以通过将枚举的原始值分配给Int值并将这些值比较如下来实现:

data.sort { $0.workout.difficulty!.rawValue < $1.workout.difficulty!.rawValue }
Run Code Online (Sandbox Code Playgroud)

但我真的希望这个枚举存储字符串,因为将它分配给标签文本很方便,没有丑陋的开关盒黑客,并且在某些方面具有可比性.

怎么实现呢?

swift

6
推荐指数
2
解决办法
3128
查看次数

Swift 3:Realm创建了额外的对象,而不是更新现有的对象

在我的AppDelegate中

let realm = try! Realm()
    print("number of users")
    print(realm.objects(User.self).count)
    if !realm.objects(User.self).isEmpty{
        if realm.objects(User.self).first!.isLogged {
            User.current.setFromRealm(user: realm.objects(User.self).first!)
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let viewController = storyboard.instantiateViewController(withIdentifier :"TabBar") as! CustomTabBarController
            self.window?.rootViewController = viewController
        }
    } else {
        try! realm.write { realm.add(User.current) }
    }
Run Code Online (Sandbox Code Playgroud)

我只在应用程序中没有用户对象时才创建用户

由于这个答案,我以下面的方式更新我的对象

public func update(_ block: (() -> Void)) {
    let realm = try! Realm()
    try! realm.write(block)
}
Run Code Online (Sandbox Code Playgroud)

但事实证明它创建了新的User对象.如何始终更新现有的而不是创建新对象?

请注意我使用,User.current因为我的对象是单例

登录和注销后,它会打印用户数= 2,这意味着更新现有用户会创建一个新用户

realm ios swift swift3

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

Swift - 如何根据异步下载的数据创建表视图?

我的应用程序从网上下载json,用引脚绘制地图并在表格视图中显示数据.

Map不是问题,因为我编写自定义函数来绘制地图并在dispatch_async之后立即调用它(dispatch_get_main_queue()行完美地运行 - map正在等待数据下载.但是,问题出现在我想要的时候对表视图执行相同操作.

我的代码很长,所以我将只展示简短的片段来了解问题的想法.

我尝试了以下修改:

    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")
                // Do any additional setup after loading the view, typically from a nib.
                self.tableView.backgroundColor = UIColor.clearColor();
                self.tableView.delegate = self;
                self.tableView.dataSource = self;
Run Code Online (Sandbox Code Playgroud)

但遗憾的是它无论如何都没有帮助 - 在下载数据之前,表视图再次加载,从而导致空表.

表视图使用自己的预定义函数,例如:

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
Run Code Online (Sandbox Code Playgroud)

如果没有错误,将它们置于异步任务中是不可能的

我的问题是:如何处理这种东西(我的意思是当你想要一个元素如表视图"等待"数据首先被下载而你不能把它放在下载任务中)?

我希望我能够相对清楚地提出问题,提前谢谢

cocoa-touch ios swift

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

Swift - 将圆角设置为注释视图图像

这是我之前的帖子,我将图像设置为注释视图引脚

Swift - 将不同的图像从数组设置为注释引脚

现在我遇到了在注释视图图像上设置圆角的问题.

cannot show callout with clipsToBounds enabled swift
Run Code Online (Sandbox Code Playgroud)

似乎我必须在圆角或显示标注之间做出选择,我真的不明白为什么这两个不能出现.有没有办法做到这一点?这是我的viewForAnnotation函数:

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if !(annotation is RestaurantAnnotation) {
        return nil
    }

    let reuseId = "restaurant"
    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView.canShowCallout = false
    }
    else {
        anView.annotation = annotation

    }

    let restaurantAnnotation = annotation as RestaurantAnnotation

    if (restaurantAnnotation.image != nil) {

                        anView.image = restaurantAnnotation.image!

                        anView.layer.masksToBounds = true

                        anView.layer.cornerRadius = (anView.frame.size.height)/10.0

    } …
Run Code Online (Sandbox Code Playgroud)

mapkit mkannotation mkannotationview ios swift

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

Swift 3:reverseGeocodeLocation不会调用其完成处理程序

这是我的代码

if loc.latitude != 0.0 && loc.longitude != 0.0 {
    let loca = CLLocation(latitude: loc.latitude, longitude: loc.longitude)
    geoCoder.reverseGeocodeLocation(loca) { (placemarks, error) in // this is the last line that is being called
      var placemark : CLPlacemark!
      placemark = placemarks?[0]
      city = (placemark.addressDictionary?["City"] as! String)
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的应用程序中执行此代码段是正确的,没有发生运行时错误.

但是,被调用的最后一行是

geoCoder.reverseGeocodeLocation(loca){(placemarks, error)

我也仔细检查过那loca不是零.

为什么没有调用完成处理程序?

reverse-geocoding cllocation ios swift swift3

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