Programmatically Set UIImage Animation with WatchKit in Swift - swift

I can't seem to figure out how to programmatically animate UIImages.
I've seen a code written in Objective-C, but looking for a solution in Swift.
The closest code I can find is:
imageView.startAnimatingWithImages(in: ???[NSRange] , duration: 1, repeatCount: 100)
but I'm not sure what to put in NSRange. Images is stored in my asset folder in a folder:
enter image description here
Code in swift(but not for Apple Watch): How to animate images in Swift?
Objective- C code: Programatically Set UIImage Animation with WatchKit

Found a swift solution: quiet simple, I just needed to add an image outlet and write the following two code items:
class InterfaceController: WKInterfaceController {
#IBOutlet weak var imageObject: WKInterfaceImage!
override func awake(withContext context: Any?) {
super.awake(withContext: context)
imageObject.setImageNamed("plank")
imageObject.startAnimating()
// Configure interface objects here.
}
}

Related

NSTextView not being found in project scope SwiftUI

I'm trying to add a TextEditor to my project with a different background color. I'd like to just use the simple extension that's found in this post: Transparent Background for TextEditor in SwiftUI
The problem is this--when I paste it in, I get the following errors as seen below:
extension NSTextView { // Cannot find type 'NSTextView' in scope.
open override var frame: CGRect {
didSet {
backgroundColor = .clear // Cannot find 'backgroundColor' in scope.
drawsBackground = true // Cannot find 'drawsBackground' in scope.
}
}
}
I assumed NSTextView was a built-in Swift type because there's documentation here (https://developer.apple.com/documentation/appkit/nstextview). I'm importing SwiftUI and Foundation at the top of the file. I'm trying to compile on an iPhone target set to iOS 14.4 so this shouldd definitely be working, right? What am I missing?

How to use NSTextFinder with NSOutlineView?

There is not much information about how to use NSTextFinder and most of them are about NSTextView. I am interested to learn how to use it with NSOutlineView. This is what I have done.
On storyboard, I dragged an NSTextFinder onto a view controller where it contains an NSOutlineView. I connected the client to the view controller and the find bar container to the NSScrollView that encloses the NSOutlineView. I made an #IBOutlet to the NSTextFinder and enabled incremental searching and dim content view.
My code looks something like this.
class ViewController: NSViewController {
#IBOutlet var outlineView: NSOutlineView!
#IBOutlet var textFinder: NSTextFinder!
// ...
#IBAction func findSomething(_ sender: Any?) {
print("findSomething(_:) was called!")
textFinder.performAction(.showFindInterface)
}
}
I connected findSomething(_:) as a first responder to one of the NSMenuItem. The method is called and a find bar actually appear.
How do I actually get it to search strings in the NSOutlineView? I am not interested in find-and-replace. I just want it like Safari find bar.
Apple documentation on NSTextFinder is very hard to understand and there is no example code I could look into.
willeke is correct: Make your viewController, which owns the NSOutlineView, the client of the NSTextFinder and implement the NSTextFinderClient protocol. For an outline view, you implement the methods func stringLength() -> Int and func string(at: Int, effectiveRange: NSRangePointer, endsWithSearchBoundary: UnsafeMutablePointer<ObjCBool>) -> String - you want to give the NSTextFinder the illusion that there's one giant string, broken into pieces.
Example when it asks for the piece that contains location 512, you return the string from 500 to 600, and store NSMakeRange(500, 100) into its effecive range argument.
When it actually finds something, it will call your selectedRanges setter for you to move the selection there.
To make this work. You'll need to do some work up front: to provide the illusion that your document is one giant range, you'll need to build a data structure that lets you map a range from NSTextFinder to a piece of your data.

How do you get a text field input in you view controller code?

I’m trying to make Xcode print "Nice!" when you type in "Hi". I've used a IBOutlet, but I don’t know how to use the user input in my code. Also BTW I'm using Storyboard and not SwiftUI. It also gives me an error when I try to compare the datatype UIViewController and a String. Here is my view controller code(with the default App Delegate and Scene Delegate code):
import UIKit
class ViewController: UIViewController {
#IBOutlet var yeet: [UITextField]!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func fuel(_ yeet:UIViewController) -> Int {
if yeet == ("hi") {
print("Nice!")
}
}
}
your textfield show be setup as
#IBOutlet weak var textFeildName: UITextField!
you will need to change a couple things inside of your file to prevent a crash. I'd delete the textfield and drag it into the assistant view and give it a new name.
but before you press "connect" press the "outlet" tab and change it to "Action" and then a new selector should come up select "Editing Did End" and go to the top and press "Did End On Exit"
after that is done would want to reference the variable of the text field:
example:
#IBAction func TextFieldName(_ sender: Any) {
if(self.TextFeildName.text!.contains("hi")){
print("Nice!")
}
}
On top of all this, you do not compare strings with == that's only if you compare 2 separate strings for example stringOne == stringTwo if you are comparing or asking if a string contains anything you'd want to use the developing language specific string container IE: .contains
Also, please do not include "Xcode" as a tag with your question, as that should be reserved for Xcode related problems. not Swift or objective-c coding issues.

Swift - Cropping images *outside* of allowsEditing

I have a very very simple project set up that allows you to click a "browse photo" button. The user then selects a photo from their photo gallery, and it's displayed on a programmatically created UIImageView.
Works like a charm. However - I am missing key functionality that is required.
I need the user to be able to scale the image (via pinching and dragging) after it is displayed within the UIImageView. allowsEditing = true, lets the user crop before. I need similar functionality, however, allowing them to edit once it's on the main UI.
Help is appreciated. Please and thank you!!
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView!
var imageViewLayer: CALayer{
return imageView.layer
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
imageViewLayer.contents = UIImage(named: "ss3.jpg")?.CGImage
imageViewLayer.contentsGravity = kCAGravityResizeAspect
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func newGesture(sender: AnyObject) {
imageViewLayer.transform = CATransform3DMakeScale(sender.scale, sender.scale, 1)
}
}
I did something similar a while back. I added the image to UIImageView's layer property, added gesture recognizer to the view and implemented the gesture call backs modifying the layer property and not the view. Adding the image to the UIImageView's layer did the trick. As a side note, I would like to add that every UIView is supported by CALayer class. It has a lot of methods and properties which help to dynamically change the view, which in your case will be done by gestures.
As an alternative, you can also use CALayer's hitTest method instead of implementing the call backs for gesture recognizers.
EDIT- Sample Code
You could do some thing like this:
#IBOutlet weak var imageView: UIImageView!
var imageViewLayer: CALayer{
return imageView.layer
}
In the viewDidLoad, set up the image
imageViewLayer.contents = UIImage(named: "CoreDataDemoApp")?.CGImage
imageViewLayer.contentsGravity = kCAGravityResizeAspect
Add pinch gesture to the imageview in storyboard (or programmatically) and in it's call back you could do something like this:
#IBAction func pinchGestureRecognized(sender: AnyObject) {
imageViewLayer.transform = CATransform3DMakeScale(sender.scale, sender.scale, 1)
}
Again this is just to give you an idea of how it could work and it is not the complete code. Hope this helps!
This is another way of doing it:
Stackoverflow link to related question

swift iMessage style keyboard input

i am trying to implement an iMessage style keyboard input with a textview that sits at the bottom of the screen then slides up with the keyboard when you touch the textView, then is docked to the top of the keyboard.
I found MessageComposerView which is exactly what I want. Unfortunately I cannot get it working ( I am using swift).
below is my code:
import UIKit
class CommentsViewController: UIViewController, MessageComposerViewDelegate {
var messageComposerView: MessageComposerView!
override func viewDidLoad() {
super.viewDidLoad()
let defaultWidth = view.frame.size.width
let defaultHeight = CGFloat(54.0)
let subviewFrame = CGRect(x: 0, y: view.frame.height - defaultHeight, width: defaultWidth, height: defaultHeight)
messageComposerView = MessageComposerView(frame: subviewFrame) as MessageComposerView
view.addSubview(messageComposerView)
}
func messageComposerSendMessageClickedWithMessage(message: String!) {
}
}
however it does not show up. Ive printed the view and its frame is correct, its just that there is nothing there for some reason. Everything looks like it should be working. Does anyone see anything wrong with my current implementation?
Try PHFComposeBarView Library (https://github.com/fphilipe/PHFComposeBarView), it's a exact copy of the iMessage composer bar that can be used in C & Swift from storyboard or code
Code Example : https://github.com/liveminds/SwiftPHFComposeBarTest
Storyboard Example : https://github.com/liveminds/SwiftPHFComposeBarTest/tree/storyboard-managed
To add the bar to your view:
drag a new UIView on your UIViewcontroller, assign "PHFComposeBarView" class to UIView
Add an outlet of the UIView to your Viewcontroller's class
add "PHFComposeBarViewDelegate" to your Viewcontroller's class
assign UIView Delegate in viewdidload() : self.composerBarOutlet.delegate = self
Set the composer bar to appear as inputAccessoryView above the keyboard:
override var inputAccessoryView: UIView {
composerBar.removeFromSuperview()
return composerBar
}
Style your Bar(example):
composerBar.utilityButtonImage = UIImage(named: "fullStar")!
composerBar.buttonTitle = "Submit"
composerBar.maxCharCount = 200
composerBar.maxLinesCount = 5
composerBar.alpha = 1.0
composerBar.buttonTintColor = AppConfig.BLUECOLOR
composerBar.placeholder = "What do you think about this product?"
Try following this example by Andrew Bancroft Send Text Message In-App – Using MFMessageComposeViewController with Swift. His example provides a nice walkthrough using Swift and includes sample code on GitHub.
Just going from the code snippet included in your post, it looks like you need to import Foundation, import MessageUI, conform to the MFMessageComposeViewControllerDelegate protocol, and implement the messageComposeViewController protocol method. All of this is covered in Andrew's blog post. This should give you what you're looking for.
Have you find the answer yet?
I use the same Framework however I believe you need to set up the delegate to self. IN my case it fails though I dont know why but it is written down in the readme github.