-rw-r--r-- 577B Oct 28, 2016 · 7DC1233 · ~1 min

Shake effect animation for UIView

шпаргалки swift
// MARK: - UIView Extension, or NSView
extension UIView {

    /// Shake animation
    func shake() {
        let animation = CAKeyframeAnimation(keyPath: "transform.rotation")
        animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear)
        animation.duration = 1.0
        animation.values = [-Double.pi / 6, Double.pi / 6, -Double.pi / 6, Double.pi / 6, -Double.pi / 7, Double.pi / 7, -Double.pi / 8, Double.pi / 8, 0.0 ]

        layer.add(animation, forKey: "shake")
    }

}

myView.shake()

[↵] open page shake-effekt-dlya-uiview.md
-rw-r--r-- 3.2K Sep 29, 2016 · E72E8DF · ~3 min

On Migrating to Swift 3 and Swift 2.4

swift

Over the past couple of weeks, new versions of macOS, iOS, tvOS, and watchOS have been released. Along with them came a new Xcode and a new version of Swift.

When you open a project in the new Xcode, it immediately offers to convert the project to the new Swift version—either to version 2.3, which is almost unchanged as an intermediate version, or straight to the new Swift 3, which has quite a lot of changes.

Converting the project itself is not difficult, because Xcode has a built-in converter. I decided to try 2.3 first so as not to dive into the weeds of the changes. The working project converted without problems, and then dependency hell began. I use Carthage; all dependencies are compiled into a .framework that you import into your project. Once imported, you usually do not need to touch the project itself anymore, just rebuild the dependencies. In the first week after the release, many developers quickly shipped updates—a release for Swift 2.3 and a release for Swift 3. But not everyone. For example, SwiftyJSON: its developers disappeared for a while, and the framework is very popular. I had to dig around—the dependencies that had not been updated were fortunately picked up by third-party developers. They forked them, moved everything to Swift 2.3, and sent pull requests to the original repositories.

I had to spend some time replacing some dependencies with forks. And hooray, the project finally built. During the following week, the authors of all the dependencies I use had already shipped Swift 2.3 releases. With a clear conscience, I switched the dependencies back to the originals.

Naturally, it made sense to move to Swift 3 as soon as possible, because 2.3 is a temporary version and support for it will disappear in future Xcode releases. Besides, Swift 3 was promised to be, if possible, the last version that would break compatibility with previous versions.

Again, the project itself converted to Swift 3 without major issues. Not everything, but a lot. And whatever did not convert automatically, Xcode itself suggested fixes during compilation; for the most part I only had to click the mouse and let it apply them. Even though I waited until all dependencies were updated to Swift 3, I still had to sweat a bit. Incidentally, if a framework is compiled in Swift 2.3, you cannot include it in a Swift 3 project. And vice versa too. I cleared up various problems—and then a different kind of headache started. AlamoFire for Swift 3 did not just get updated, it got a major update—with renamed classes and API changes. As a result, I had to rewrite some methods by two thirds. Although in most cases I just had to swap argument order and rename things here and there. Plus, now to pass GET parameters, you have to add them to the URL string by hand; previously the parameters argument could do that, but now that data is sent in the request body as if we had filled in a form.

But in the end everything turned out fine, although the migration took time and effort. Onward, into the future.

P.S. The open-source version of Swift 3, which can run on Linux, still does not have a full implementation of the classes for working with networking. And the backend framework Perfect does not compile on Ubuntu 16.04. So Swift for the backend is postponed again for now.

[↵] open page pro-perehod-na-swift-3-i-swift-2-4.md
-rw-r--r-- 996B Sep 26, 2016 · 55D7D8F · ~1 min

An Elegant Notification.Name Extension in Swift 3

swift шпаргалки

In the new Swift 3 there is now a different initializer for NSNotification (now simply Notification; the NS prefix was dropped):

struct Config {
    static let ShouldCloseBrowserNotification = "ShouldCloseBrowserNotification"
}

let notification = Notification(
    name: Notification.Name(rawValue: Config.ShouldUpdateDialogNotification),
    object: nil,
    userInfo: nil
)
NotificationCenter.default.post(notification)

The name parameter does not look very nice now. But you can extend Notification.Name to make it look better:

extension Notification.Name {
    static let shouldCloseBrowserNotification = Notification.Name("ShouldCloseBrowserNotification")
}

let notification = Notification(
    name: .ShouldUpdateDialogNotification,
    object: nil,
    userInfo: nil
)
NotificationCenter.default.post(notification)

UIKit itself is structured in roughly the same way:

UIKit code

[↵] open page elegantnyj-extension-dlya-notification-name-v-swift-3.md
-rw-r--r-- 1.3K Sep 25, 2016 · 987C1A8 · ~1 min

About the Mac App Store

appmetric mac app store приложения

I had read mentions before that the Mac App Store is dead. About a month ago I hacked together my informer for Yandex AppMetrica and uploaded it to MAS. I started watching the charts (not for long, admittedly). In fact, there were 2 purchases at launch (I even know who both were from). The result surprised me.

Mac App Store home page

Immediately first place in the Business category in the Russian Mac App Store. Of course, it is nice when your icon decorates the list of categories.

Mac App Store categories

Still, just 2 purchases say that people really buy and download very few apps in the Mac App Store. Maybe free ones get downloaded more, but paid ones definitely do not. At least in Russia. It is nothing like the App Store for iOS apps. On top of that, the app reached 8th place in the store's overall top chart.

Mac App Store top chart

So it is not about the category. In other countries, apps are probably bought more often, but in Russia it is very little. Then again, piracy is much more widespread on macOS than on iOS, because there is no jailbreak hassle involved. That is probably why people here do not buy very often.

[↵] open page pro-mac-app-store.md
-rw-r--r-- 452B Aug 28, 2016 · FD72C26 · ~1 min

AppMetric for macOS

appmetric appmetrica macos приложения

Over a couple of evenings I made a simple macOS informer that shows app stats from Yandex AppMetrica. It lives as an icon in the status bar:

  • Shows a list of all apps from AppMetrica.
  • Displays the number of users, sessions, and crashes for today.
  • Automatically refreshes stats in the background.

Download

[↵] open page appmetric-dlya-macos.md
makoni@arm1:~/blog$ cd ../page-9/ // ← previous cd ./page-11/ // more posts →