Trying animate series of images in swift 2.0 but following the video tutorials like https://www.youtube.com/watch?v=PSny6un3VUw the method that this guy uses are no longer relevant and swift 2.0 no longer allows arrays to be setup in such means. The basic code this guy used is as follows:
class ViewController: UIViewController: {
#IBOutlet var myImageView: UIImageView
#IBOutlet var animationBtn: UIButton
var imageList = UIImage[]() // no longer valid?
#IBAction func animationBtnClicked(sender: AnyObject) {
startAnimation
}
override func viewDidLoad() {
for i in 1...13
{
let imageName = "\(1)"
imageList += UIImage(named: imageName) //line no longer works
}
}
func startAnimation() -> Void
{
myImageView.animationImages = imageList
myImageView.startAnimation()
}
Anyone know a better way in swift 2.0 to animate images or anyway in general how to do this?
I hope I will help you
#IBOutlet var myImageView: UIImageView!
#IBOutlet var animationBtn: UIButton!
var imageList = [UIImage]()
imageList.append(UIImage(named: imageName))
or
imageList += [UIImage(named: imageName)]
Related
mask remove after zoommask imageI want to zoom the masked image.
I can display the mask image but when i try to zoom the image then mask is not working.
I want to zoom the image in mask area only.
Please check the code below. its working for me.
class ViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrolView: UIScrollView!
#IBOutlet weak var imgPhoto: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
scrolView.delegate = self
scrolView.minimumZoomScale = 1.0
scrolView.maximumZoomScale = 10.0
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imgPhoto
}
}
I am new to Mac OSX and with Apple promoting the fact that the bodies of code are becoming similar decided to tell the folk I am writing code for we should be able to do a Mac OSX version. iPhone and iPad versions are all good and about to release second version so no issues there.
So I am subclassing NSWindowController to get access to the Toolbar and worked out how to remove and add items on the toolbar, but for the life of me I can not get one NSViewController (firstViewController) to dismiss and bring up the second NSViewController (secondViewController) in the same NSWindowController.
So the 2 issues are that
1. I want to be able to performSegueWithIdentifier from the first NSViewController in code and
2. bring up the second NSViewController by replacing the first NSViewController in the same NSWindowController.
If I add a button to the firstViewController and put a segue to the secondViewController then when I select the button the secondViewController comes up just fine but in a seperate window not the same NSWindowController that I want it to and the firstViewController does not get replaced but stays in the NSWindowController.
So I know the segue idea will work but its not working in code and when I do insert the segue from a button it works but into a seperate NSViewController that is not part of the NSWindowController.
I am trying to find some programming guide from Apple on the issue but no luck so far.
Here is an overview from my Storyboard:
Here is my NSWindowController subclassed and the func loginToMe2Team is trigger from the NSToolBar and its working just find as the print statements show up on the console.
import Cocoa
class me2teamWindowsController: NSWindowController {
#IBOutlet var mySignUp : NSToolbarItem!
#IBOutlet var myToolbar : NSToolbar!
let controller = ViewController()
override func windowDidLoad() {
super.windowDidLoad()
print("window loaded")
}
override func windowWillLoad() {
print("window will load")
}
#IBAction func logInToMe2Team(sender: AnyObject){
controller.LogIn() //THIS IS THE FUNC I AM TESTING WITH
}
#IBAction func signUpToMe2Team(sender: AnyObject){
controller.signUp()
}
Here is my NSViewController subclassed with the func LogIn. Its getting selected just fine but the performSegueWithIdentifier is not. And I did cut and past the Identifier to make absolutely sure it was the same.
import Cocoa
import WebKit
class ViewController: NSViewController {
#IBOutlet weak var theWebPage: WebView!
#IBOutlet weak var progressIndicator: NSProgressIndicator!
override func viewDidLoad() {
super.viewDidLoad()
let urlString = "https://thewebpage.com.au"
self.theWebPage.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: urlString)!))
}
override func viewDidAppear() {
}
func LogIn() {
print("I logged in")
self.performSegueWithIdentifier("goToTeamPage", sender: self)
//THIS IS THE BIT THATS NOT WORKING
}
func signUp() {
print("I have to sign up now")
}
override var representedObject: AnyObject? {
didSet {
}
}
func webView(sender: WebView!, didStartProvisionalLoadForFrame frame: WebFrame!)
{
self.progressIndicator.startAnimation(self)
}
func webView(sender: WebView!, didFinishLoadForFrame frame: WebFrame!)
{
self.progressIndicator.stopAnimation(self)
}
}
You need to use a custom segue class (or possibly NSTabViewController if it’s enough for your needs). Set the segue’s type to Custom, with your class name specified:
…and implement it. With no animation, it’s simple:
class ReplaceSegue: NSStoryboardSegue {
override func perform() {
if let src = self.sourceController as? NSViewController,
let dest = self.destinationController as? NSViewController,
let window = src.view.window {
// this updates the content and adjusts window size
window.contentViewController = dest
}
}
}
In my case, I was using a sheet and wanted to transition to a different sheet with a different size, so I needed to do more:
class ReplaceSheetSegue: NSStoryboardSegue {
override func perform() {
if let src = self.sourceController as? NSViewController,
let dest = self.destinationController as? NSViewController,
let window = src.view.window {
// calculate new frame:
var rect = window.frameRectForContentRect(dest.view.frame)
rect.origin.x += (src.view.frame.width - dest.view.frame.width) / 2
rect.origin.y += src.view.frame.height - dest.view.frame.height
// don’t shrink visible content, prevent minsize from intervening:
window.contentViewController = nil
// animate resizing (TODO: crossover blending):
window.setFrame(window.convertRectToScreen(rect), display: true, animate: true)
// set new controller
window.contentViewController = dest
}
}
}
I want to add an image to a button, and when it is clicked, i want it to change. I'm very new to Swift so I know very little.
I have it like this:
//
// ViewController.swift
// Tip Calculator
//
// Created by ios4 on 10/16/15.
// Copyright (c) 2015 ios4. All rights reserved.
//
import UIKit
class ViewController: UIViewController
{
#IBOutlet weak var tipAmount: UILabel!
#IBOutlet weak var userBillTextField: UITextField!
#IBOutlet weak var tenOutlet: UIButton!
override func viewDidLoad()
{
tipAmount.text = " "
// var billDouble = (userBillTextField.text as NSString).doubleValue
super.viewDidLoad()
}
#IBAction func calculateButton(sender: UIButton)
{
var billDouble = NSString(string: userBillTextField.text).doubleValue
tipAmount.text = String( stringInterpolationSegment: billDouble * 0.15)
}
#IBAction func tenButton(sender: UIButton)
{
}
and I want the outlet called ten outlet to change to a different image called 10_selected_image from 10_unselected_image.
Any help?
Here are my instructions for this : https://s3.amazonaws.com/mmhighschool/MasterDocs/TipCalculator/TipCalculatorAppDirections.pdf
I'm currently working on stretch 3
You should be able to use set image for control state in your button action
button.setImage(image: UIImage(named: <image name>), forState: UIControlState.Normal)
If you need more info about how to make a "toggle button" this thread has a good answer to that
How to use a UIButton as a toggle switch
For me in Swift 5 is working like:
boton1.setBackgroundImage(UIImage(named: "image Name"), for: UIControl.State.normal)
Is it possible to change a value in Cocoa depending on the direction of the trackpad scroll/ magic mouse 'wheel' scroll etc.
For example
var value = 0
func startsScrolling() {
if direction == up {
value += 5
} else if direction == down {
value -= 5
}
}
This would continuous so as you scroll up on the trackpad the direction is down and so it takes 5 off every time the fingers move up, and vice versa.
Thanks and sorry for the bad pseudo code.
You can create a custom window and override scrollWheel NSEvent. You can check the scrollingDeltaX and scrollingDeltaY properties.
class CustomWindow: NSWindow {
override func scrollWheel(theEvent: NSEvent) {
println( "Mouse Scroll scrollingDeltaX = \(theEvent.scrollingDeltaX)" )
println( "Mouse Scroll scrollingDeltaY = \(Int(theEvent.scrollingDeltaY))" )
}
}
If you are using storyboards you have to override scrollWheel NSEvent of your viewController:
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var strDeltaX: NSTextField!
#IBOutlet weak var strDeltaY: NSTextField!
#IBOutlet weak var strScrollingDeltaX: NSTextField!
#IBOutlet weak var strScrollingDeltaY: NSTextField!
#IBOutlet weak var strValueX: NSTextField!
#IBOutlet weak var strValueY: NSTextField!
var xValue:CGFloat = 0
var yValue:CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
}
override var representedObject: AnyObject? {
didSet {
}
}
override func scrollWheel(theEvent: NSEvent) {
strDeltaX.stringValue = "deltaX: \(theEvent.deltaX)"
strDeltaY.stringValue = "deltaY: \(theEvent.deltaY)"
strScrollingDeltaX.stringValue = "scrollingDeltaX: \(theEvent.scrollingDeltaX)"
strScrollingDeltaY.stringValue = "scrollingDeltaY: \(theEvent.scrollingDeltaY)"
xValue += theEvent.scrollingDeltaX
yValue += theEvent.scrollingDeltaY
strValueX.stringValue = "xValue: \(xValue)"
strValueY.stringValue = "yValue: \(yValue)"
}
}
First you need to add a scroll view to your ViewController and link it. You can make it transparent if you don't want it to show. If you have buttons on your ViewController you can send it to back. I've converted code from objective-c. More power to you :)
var pointNow: CGPoint?
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
pointNow = scrollView.contentOffset
}
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView.contentOffset.y < pointNow!.y {
println("down")
}
else {
if scrollView.contentOffset.y > pointNow!.y {
println("up")
}
}
}
Im simply trying to make it so when I click on a UIButton (for which it currently shows the image of a shell), the image changes into something else (in this case, a coin).
This is what i tried so far and have not had any success. I cant find anything to do this for Swift.Thanks.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var lblOutput: UILabel!
#IBOutlet weak var lblWin: UILabel!
#IBOutlet weak var lblLost: UILabel!
#IBOutlet weak var lblWinsAmt: UILabel!
#IBOutlet weak var lblLossesAmt: UILabel!
let coin = UIImage(named: "penny_head") as UIImage;
override func viewDidLoad() {
super.viewDidLoad()
//imgShell1.hidden = true //doesnt work
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func btnStart(sender: UIButton) {
}
#IBAction func btnShell1(sender: UIButton) {
sender.setImage(coin,forState: UIControlState.Highlighted);
}
The way you're setting up the control is incorrect. Assuming you have a button property named btnShell (and it's the button you want to setup) change your viewDidLoad() method to:
override func viewDidLoad()
{
super.viewDidLoad()
btnShell.setImage(imgShell1, forState:.Normal);
btnShell.setImage(coin, forState:.Highlighted);
}
And then remove the setImage(_:forState:) call from the action method:
#IBAction func btnShell1(sender: UIButton) {
sender.setImage(coin,forState: UIControlState.Highlighted);
}
To permanently change the button image on tap, you have to use the .Normal enum case and not .Highlighted for the control state:
sender.setImage(coin,forState: UIControlState.Normal)
Setting the image for the .Highlighted state makes the new image appear only when the button is in that state, i.e. when it is tapped.
If you are looking to change the UIButton's background image permanently you have to use Antonio's method:
sender.setImage(coin,forState: UIControlState.Normal)
This won't change the UIKit's default highlighting when you tap the button. When you don't want the button to be highlighted when you touch it or have different appearances for different states, then you might be better off using an UIImageView.
In viewDidLoad():
imgView?.image = imgOne
imgView?.userInteractionEnabled = true
imgView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "changeImage:"))
The function that changes the image:
func changeImage(recognizer: UITapGestureRecognizer){
self.imgView?.image = imgTwo
}
You can also use isSelected.
button.setImage(image2, for: .normal)
button.setImage(image1, for: .selected)