I can't find an updated article that shows how to show a live preview of my UIView animations. Methods like XCShowView() aren't used in Swift anymore, according to the error I received in the playground editor.
Does anyone know how to show the prototyping preview in Playgrounds?
This works in Xcode 8:
import UIKit
import PlaygroundSupport
let v = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 600, height: 600)))
v.backgroundColor = UIColor.green
PlaygroundPage.current.liveView = v
Create a playground with that code and you should see a green box on the live view.
Remember to show the "live" assistant editor. See PlaygroundSupport reference for more.
Related
I maintain an app SDK that contains a button which should always be displayed on top of all screens in the application, but this functionality broke with iOS 13+ which seems to have changed some internals. The button disappears after changing from the first screen.
// My code that does not work anymore
var applicationWindow: UIWindow? = UIApplication.shared.keyWindow
...
button.frame = CGRect(x: 0, y: 0, width: 94, height: 73)
applicationWindow!.addSubview(button)
I saw similar posts on SOF suggesting code like
UIApplication.shared.windows.first(where: { $0.isKeyWindow })? but it doesn't work for me, and I'm struggling to create and manage a second UIWindow, any code sample would be really appreciated as I'm only a maintainer and I don't work actively on iOS anymore.
I have some trouble importing my animation from Adobe After Effect to Xcode and web.
I have made a character and rigged it with the Duik-plugin. I made the annimation i would like to get into xcode, and rendered it to JSON using bodymovin.
I have an image of the animation here:
Then i take the rendered JSON file and import it into my project. I use the Lottie-ios framework (https://github.com/airbnb/lottie-ios).
In my viewDidLoad() i use this code to show the animation:
let animationView = LOTAnimationView(name: "Velkomst")
animationView.frame = CGRect(x: 0, y: 100, width:
self.view.frame.size.width, height: 250)
view.addsubview(animatedView.contentMode = .scaleAspectFill
animationView.play()
But as you can see below, the result is pretty... Wierd...
I also created a demo file when i rendered the after effect aniation. On the demo-website it is a little closer to the animation. But still not there.
Can anyone tell me what i am doing wrong? Cause i cant figure it out :-(
I am wondering if I can use UI elements like UIButton, UILabel in an augmented reality app with ARKit.
If you are also interested in transparency modes for that UIView subclasses try my sample https://github.com/erikhric/ar-menu
You can use different blending modes. I guess .alpha will work for your purposes.
Yes, you can use UIKit elements by adding them to a UIView that's positioned above the view displaying the AR scene (ARSKView or ARSCNView).
If you create a new project in Xcode and select the "Augmented Reality App" template, you can see that the AR content is just a view like any other UIKit view.
What worked best for me
in main.storyboard:
- delete SceneView
- add regular UIView
- add ARKit SceneKit View on top of that
- then you can add buttons, etc.
Yes you can place UI elements on top of the ARSKView or ARSCNView displaying the AR scene:
let scanningPanel = UIImageView()
scanningPanel.backgroundColor = UIColor(white: 0.33, alpha: 0.6)
scanningPanel.layer.masksToBounds = true
scanningPanel.frame = CGRect(x: -2,
y: self.sceneView.frame.height-270,
width: 178,
height: 50)
scanningPanel.layer.cornerRadius = 10
let scanInfo = UILabel(frame: CGRect(x: 8,
y: self.sceneView.frame.height-268,
width: 160,
height: 45))
scanInfo.textAlignment = .left
scanInfo.font = scanInfo.font.withSize(15)
scanInfo.textColor = UIColor.white
scanInfo.text = "SCAN A SURFACE"
Adding:
self.sceneView.addSubview(scanningPanel)
self.sceneView.addSubview(scanInfo)
Removing:
if(scanInfo.isDescendant(of: self.sceneView)) {
scanInfo.removeFromSuperview()
}
You can insert content of any view on a plane in ARKit like this:
let plane = SCNPlane(width: sceneView.bounds.width/3000,
height: sceneView.bounds.height/3000)
plane.firstMaterial?.diffuse.contents = self.anyView`
Gestures and taps are automatically sent to that view.
Try my example.
I didn't find any related question on the web, and I am trying to get a blur view to display the new blur effects .prominent and .regular but they are not showing. When I change the blur effect to .light, .extraLight or.dark, it works fine. It says in the description that the new blur effects adapts to the user interface. What does that mean, and why aren't those two new blur effects working?
I have iOS 10 in both simulator and in my iPhone and none of them are displaying the new blur effect. Print statements say that the if statement (instead of the else) is being called, as expected.
let blurEffect : UIBlurEffect!
if #available(iOS 10.0, *) {
blurEffect = UIBlurEffect(style: .prominent )
} else {
// Fallback on earlier versions
blurEffect = UIBlurEffect(style: .light )
}
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = CGRect(x: 100, y: 100, width: 200, height: 300)
What does that mean, and why aren't those two new blur effects working?
From WWDC 2016 Session 206: What's New in tvOS :
We've also added two new blur styles to the API.
You can now use UIBlurEffectStyleRegular or UIBlurEffectStyleProminent.
And we call these automatic styles.
And they'll actually adjust the effective blur effect style based on what the system setting is.
So if you use UIBlurEffectStyleRegular and the system's in light, it will use UIBlurEffectStyle.light.
If you use regular and dark, you'll use dark.
If you use prominent, it will use .extraLight and .extraDark.
.extraDark will be coming in a later seed
See all text of the session: http://asciiwwdc.com/2016/sessions/206
Basically this effects are for tvOS. That OS can be in dark or light style. For iOS those effects work in light mode.
I am a newbie in iOS Development.
I need to create a view which is quite complex in layout and I need to do create it programatically only.
There are labels, scrollviews, etc. in that view.
Right now in order to set the labels properly, I need to run simulator lot many times. This is slowing my development process.
I want to know if there is a way to easily set the labels/view without running simulator continuously, when we are creating views via code (no XIB).
I would really appreciate if someone can point me in the right direction.
Thank you!
I use playgrounds when I want to rapidly prototype a view class. When using the view preview functionality you can create a view with whatever frame you want to display on. Here's a very basic example view being displayed on a iPhone 5s screen:
//: Playground - noun: a place where people can play
import Foundation
import UIKit
class MainView : UIView {
var contentContainer : UIScrollView
var header : UILabel
var content : UILabel
var image : UIImageView
override init(frame: CGRect) {
contentContainer = UIScrollView(frame: CGRect(origin: CGPoint.zero, size: frame.size))
contentContainer.backgroundColor = UIColor.whiteColor()
header = UILabel(frame: CGRect(x: 10, y: 10, width: frame.width-20, height: 30))
header.font = UIFont.systemFontOfSize(20, weight: UIFontWeightBold)
header.text = "MY VIEW HEADER"
header.textAlignment = NSTextAlignment.Center
header.textColor = UIColor.grayColor()
contentContainer.addSubview(header)
image = UIImageView(frame: CGRectMake(0, 0, 200, 100))
image.center = CGPoint(x: frame.width/2, y: header.frame.origin.y + header.frame.size.height + 10 + image.frame.size.height/2)
image.backgroundColor = UIColor.orangeColor()
contentContainer.addSubview(image)
content = UILabel(frame: CGRect(x: 20, y: image.frame.origin.y + image.frame.size.height + 20, width: frame.width-40, height: 110))
content.text = "Bacon ipsum dolor amet flank kielbasa drumstick, ham tongue pancetta shank. Shankle tenderloin filet mignon andouille doner short ribs meatball frankfurter. Ham boudin tri-tip porchetta fatback, bresaola landjaeger kielbasa brisket pork belly bacon picanha alcatra ham hock. Venison turducken boudin pork loin meatloaf spare ribs meatball biltong rump t-bone bacon ground round leberkas filet mignon. Shankle meatloaf ham hock strip steak porchetta."
content.numberOfLines = 0
content.font = UIFont.systemFontOfSize(10, weight: UIFontWeightLight)
contentContainer.addSubview(content)
contentContainer.contentSize = CGSizeMake(contentContainer.frame.width, content.frame.origin.y + content.frame.height + 50)
super.init(frame: frame)
self.addSubview(contentContainer)
}
//needed to remove that annoying warning
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//now preview the resulting view by clicking the dot on the left side -->
let view = MainView(frame: CGRect(x: 0, y: 0, width: 320, height: 568))
Hope this helps!
Xcode playground mode may help you (UIKit supports).
From Apple Doc:
*A playground is an interactive Swift coding environment that evaluates each statement and displays results as updates are made, without the need to create a project.
Use playgrounds to learn and explore Swift, prototype parts of your app, and create learning environments for others. The interactive Swift environment lets you experiment with algorithms, explore system APIs, and even create custom views.
Share your learning with others by adding notes and guidance using rich comments. Create an explorable learning environment by grouping related concepts into pages and adding navigation.*
If you dont want to use simulator, then you have no other option other than using the Interface Builder. By using the interface builder, you can make the custom views IB_DESIGNABLE, so that you can just build them on the Interface Builder.
Since you are not using the interface builder to make your view, you will have to compile and run to see them in action,but don't worry, and even the interface builder preview can generate different results compared to the simulator and the device.
Now to answer directly the question, a possible solution is to use the "playground tool". The issue is that officially the "playground" can be supported only with swift (example). There are some third party projects to use playground also with objective-C(another example).
Hope to have helped in your project.
Also, if there is other solutions feel free to update the answer.