How to Zoom in on a scroll view in swift 4? - arrays

My view controller programmatically creates several image views in a grid inside a scroll view. I want to be able to pinch to zoom in/out. I have pasted some similar code below which I found on stack overflow trying to do the same thing. The issue is that viewForZooming returns only one image. How do I get pinch to zoom to work for all of the image views in the scroll view? I just want to adjust the zoom scale of the scroll view as you pinch in/out.
Following code is what I have tried to achieve the required functionality:
class PhotoViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 6.0
scrollView.delegate = self
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return image
}
}

If you want to zoom multiple image views (or other UI elements) as a whole, you need to embed them in a UIView. Then, return that view in viewForZooming.
Here's a simple example:
The Yellow rectangle is a UIView, which I've connected via #IBOutlet as myContentView. It has 3 image views as subviews.
The scroll view has a red background, not seen here because myContentView is set to equal width and height to the scroll view frame (but you'll see it in the zoomed-out screen cap below).
Here's the code:
class MultiZoomViewController: UIViewController, UIScrollViewDelegate {
#IBOutlet var scrollView: UIScrollView!
#IBOutlet var myContentView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
scrollView.minimumZoomScale = 0.5
scrollView.maximumZoomScale = 6.0
scrollView.delegate = self
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return myContentView
}
}
and the results - start / zoomed out / zoomed in:

Related

How to unmute colors in SwiftUI's NavigationSplitView sidebar

I am using NavigationSplitView, introduced in iOS16, with a basic list and attempting to color a system image with standard colors. I am noticing that when the navigationSplitViewStyle is .automatic or .prominentDetail and the color scheme is dark, that the colors are muted. I have not been able to figure out how to not-mute them, and thus stick with the original color as it's used in light mode. I'm wondering if this is possible to override? Or is there a way to drop down to UIKit and override this odd behavior?
Here is an example:
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationSplitView {
List {
ForEach([1, 2, 3], id: \.self) { item in
Button {
} label: {
HStack {
Image(systemName: "sunset.circle.fill")
.foregroundColor(.green)
Text("Item \(item)")
}
.font(.system(size: 40))
.padding()
}
}
}
} detail: {
Text("Detailed Content")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView()
.previewInterfaceOrientation(.portrait)
.preferredColorScheme(.dark)
ContentView()
.previewInterfaceOrientation(.portrait)
.preferredColorScheme(.light)
}
}
}
And you can see the different in the color between schemes here:
If you meant that in dark mode, the sun in the symbol is black and you want to keep it white, then you can achieve this by setting symbolRenderingMode modifier on the image to .palette and give it two colors, the green one as well as white using the foregroundStyle modifier.
Image(systemName: "sunset.circle.fill")
.symbolRenderingMode(.palette)
.foregroundStyle(.white, .green)
Refer to the following session from WWDC 2021 for more about using rendering modes for SF Symbols in SwiftUI: https://developer.apple.com/wwdc21/10349
If you want the colors to be exactly the same and don’t change. You can use UIKit’s colors or define your own custom colors that don’t support dark mode.
.foregroundStyle(.white, Color(uiColor: .green))
.foregroundStyle(.white, Color(red: 0, green: 1, blue: 0))
See the Fixed Colors section in this link:
https://developer.apple.com/documentation/uikit/uicolor/standard_colors

How to place Text on exact point of an Image in a ZStack SwiftUI

The image below is the output of the code I wrote. The text automatically gets centred when placing it with my image in a ZStack. But I want the Text to be placed over the 100 number on the y-axis. How can I do this? Image
Disclaimer: I am using Swift Playground
This is my Code
import SwiftUI
import PlaygroundSupport
struct ContentView : View {
var body: some View {
ZStack {
Image(uiImage: UIImage(named: "Screenshot 2020-05-12 at 14.14.22")!).resizable()
.frame(width: 700.0, height: 700.0)
Text("Hello")
.foregroundColor(Color.black)
}
}
}
PlaygroundPage.current.setLiveView(ContentView())
Here is possible approach
ZStack {
Image(uiImage: UIImage(named: "large_image")!).resizable()
.frame(width: 700.0, height: 700.0)
.overlay(GeometryReader { gp in
Text("Hello")
.foregroundColor(Color.black)
.position(x: gp.size.width / 2, y: gp.size.height / 4)
}
)
}
of course if you generate image then you should know more exact y: coordinate to position text. GeometryReader in such case gives image's local coordinate space.

Swift 4 Button fade in, out

how can I do fade in and out when button is pressed.
I am looking for most simple and best practice Swift 4 solution as possible.
Something like Apple iOS 11 Calculator button fade...
So far I have this...
myButton.setTitleColor(UIColor.red, for: UIControlState.normal)
myButton.backgroundColor=UIColor.gray
myButton.setTitleColor(UIColor.gray, for: UIControlState.highlighted)
myButton.backgroundColor=UIColor.red
I am kinda lost. Many thanks for any answer.
I don't know about fade in/out for calculator but you will get similar effect by this:
Set UIButton type as Custom and set Text Color as White from storyboard.
Set BackgroundColor of UIButton as Tungsten(take e.g. Calculator app)
Create custom class for UIButton and set customButton class to UIButton from Identity Inspector.
class customButton: UIButton {
override open var isHighlighted: Bool {
didSet {
//Set colors for Highlighted & Unhighlighted
backgroundColor = isHighlighted ? UIColor.lightGray.withAlphaComponent(0.7) : UIColor(red: 51/255, green: 51/255, blue: 51/255, alpha: 1.0)
}
}
}
Result:

UINavigationBar Custom Title View not centered on iOS11

I just updated to Xcode 9 and run to an iOS11 simulator. My custom view for the navaagation bar title is shifted down.
This code was working before i updated; it was vertically centered before
companyCountryView = CompanyNameView(frame: CGRect(x: 0, y: 0, width: Utils.getScreenWidth() - 150, height: 35))
companyCountryView.companyLbl.text = ""
companyCountryView.countryLbl.text = ""
self.navigationItem.titleView = companyCountryView
Even though I change values for y and height, no effect at all.
It seems the width value i used does not do any effect too.
I have solved it! You need to override the intrinsicContentSize in your custom view class and put the size of the view there:
class CustomView: UIView {
override var intrinsicContentSize: CGSize {
return CGSize(width: Utils.getScreenWidth() - 150.0, height: 35.0)
}
}

Google Maps GOverviewMapControl() and GAdsManager

I have a GMap2 object with a GOverviewMapControl and a GAdsManager, but they both appear in the lower right hand corner.
How can I get them to appear in different parts of the map?
Here's my Overview:
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.addControl(new GOverviewMapControl());
map.enableDoubleClickZoom();
here's my Ads
var adsManagerOptions = {
maxAdsOnMap: 2,
style: 'adunit',
channel: 'XXXXXXXXX'
};
adsManager = new GAdsManager(map, publisherID, adsManagerOptions);
adsManager.enable();
The position property of the GAdsManagerOptions class will allow you to set the position of the GAdsManager. The options are passed in to the GAdsManager constructor.
Unfortunately, the GOverviewMapControl is not as flexible. According to the documentation, "Unlike other controls, you can only place this control in the bottom right corner of the map (G_ANCHOR_BOTTOM_RIGHT)."
I have no way to test this at the moment, but your options will probably look something like:
var adsManagerOptions = {
maxAdsOnMap: 2,
style: 'adunit',
channel: 'XXXXXXXXX',
position: new GControlPosition(G_ANCHOR_BOTTOM_LEFT)
};

Resources