Clip button to view - ios

enter image description hereSo i have backgroundView (UIView) and two buttons in it.
Now i have made following code
buttonBackgroundView.layer.cornerRadius = 5.0
buttonBackgroundView.layer.borderColor = UIColor.black.cgColor
buttonBackgroundView.layer.borderWidth = 0.5
#IBOutlet weak var firstbutton: UIButton!{
didSet{
proceedButton.clipsToBounds = true
}
}
in this the view has rounded corner but the button does not have those rounded corners
How do i implement that? (so that button also get the rounded corner.
I have two buttons, so I want only the left corner of left button and right corner of right button.
I have added the image link which i want to achieve.

class ViewController: UIViewController {
#IBOutlet weak var yourView: UIView! //Create your view outlet
override func viewDidLoad() {
super.viewDidLoad()
yourView.clipsToBounds = true
yourView.layer.cornerRadius = 15 // As per you requirement
yourView.layer.borderColor = UIColor.black.cgColor
yourView.layer.borderWidth = 2
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Add Following constraint
Output:
This might help you :)

Please try using masksToBounds property,
#IBOutlet weak var firstbutton: UIButton!{
didSet{
proceedButton.clipsToBounds = true
proceedButton.masksToBounds = true
}
}

Related

swift UIVisualEffectView with mask autolayout varies by iOS device

I am working with swift UIVisualEffectView with mask option.
I am trying to make a blurred UIVisualEffectView masked by UIImageView.
I've set autolayout constraints to the views at the storyboard but the result varies through the iOS device.
Storyboard, code and results are attached below.
I would be glad if anyone help me.
I've set all auto layout constraints at the storyboard.(Capture included below)
Code is very simple. I've only set blurView.mask = topImageView.
class TestViewController: UIViewController {
#IBOutlet weak var bottomImageView: UIImageView!
#IBOutlet weak var topImageView: UIImageView!
#IBOutlet weak var blurView: UIVisualEffectView!
override func viewDidLoad() {
super.viewDidLoad()
blurView.mask = topImageView
}
// Still same issue when I move the mask to 'viewDidLayoutSubviews()' method
//
// override func viewDidLayoutSubviews() {
// super.viewDidLoad()
// blurView.mask = topImageView
// }
}
Storyboard and Code capture
Results depending on iOS versions
---
I've tried to move mask code from viewDidLoad() method to viewDidLayoutSubviews() but it's still same.
Self-solved by the below code.
Received a hint from similar issue : 'https://stackoverflow.com/questions/58998541/swift-mask-uiview-constraints-issues/59037761'
override func viewDidAppear() {
self.setupBlur()
}

Xcode how to combine UI buttons?

i'm making a custom keyboard and wanna add shadow to buttons.
so added some code(keys.layer~~) in viewDidLoad()
but i can't set multiple button to one IBOutlet
what i want is all keys have 'keys(IBOutlet)' as value(?)
is it possible? if it's not, any other thing is fine :)
help!
this is my code (pls ignore next keyboard button)
#IBAction func keys(sender: AnyObject) {
}
#IBOutlet weak var keys: UIButton!
#IBOutlet var nextKeyboardButton: UIButton!
override func updateViewConstraints() {
super.updateViewConstraints()
// Add custom view sizing constraints here
}
override func viewDidLoad() {
super.viewDidLoad()
loadInterface()
keys.layer.cornerRadius = 4.0;
keys.layer.shadowColor = UIColor.blackColor().CGColor
keys.layer.shadowOffset = CGSizeMake(0.0, 2.0)
keys.layer.masksToBounds = false
keys.layer.shadowRadius = 1.0
keys.layer.shadowOpacity = 0.5
You can make UIButton extension like this and used with all the object of your custom keyboard Button
extension UIButton {
func setShadow() {
self.layer.cornerRadius = 4.0;
self.layer.shadowColor = UIColor.blackColor().CGColor
self.layer.shadowOffset = CGSizeMake(0.0, 2.0)
self.layer.masksToBounds = false
self.layer.shadowRadius = 1.0
self.layer.shadowOpacity = 0.5
}
}
Now you can call this method with your button object like this way
self.nextKeyboardButton.setShadow()
Edit:
If you want to access multiple Button object with same object you can create an Array of UIButton, and store all the Outlet Button inside that array.
var buttonArray = [UIButton]()

Smooth and Accelerating ProgressView

I am playing with ProgressView. What I want to achieve is stopping at 33%, 66%, 100% checkpoints on button click. It's working okay if I use progressView.progress = 0.33, but it directly lands to the checkpoint. Instead, having it smooth and accelerating would look so nice.
I thought animateWithDuration would work but unfortunately it doesn't. After some reading, I found out that I can do something with NSTimer, but I couldn't achieve it.
var progressView: UIProgressView?
override func viewDidLoad()
{
super.viewDidLoad()
progressView = UIProgressView(progressViewStyle: UIProgressViewStyle.Default)
progressView?.center = self.view.center
progressView?.trackTintColor = UIColor.redColor()
progressView?.progressTintColor = UIColor.greenColor()
view.addSubview(progressView!)
progressView!.progress = 0.0
}
Using this answer, I achieved making it move once in every second, but how can I make it go smooth, slow in the beginning and accelerating, so looks nice.
using setProgress method and setting animation to true makes animation work.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var pb: UIProgressView!
#IBOutlet weak var btn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
pb.progress = 0.00
}
#IBAction func btnPress(sender: UIButton) {
UIView.animateWithDuration(3, animations: {
self.pb.setProgress((self.pb.progress + 0.33), animated: true)
}, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

iOS Swift: How to overlay two images that zoom in at same time without loosing coordinates of overlaying image

My problem is pretty simple really...
Imagine a background image that represents the streets and buildings around your house, note that this is a purpose built image. This image (a view) is zoomable and so far so good. Similar to map, but with an image instead.
Now image that on top of the streets drawn on the image there are markers representing cars. These will move over time and therefore will be moving dynamically. I have successfully placed the cars in the right place on the image, but when I zoom in/out the cars move out of place. Note that I don't want the cars to change in size, they will always remain the same.
Essentially, very similar to a map marker on top of google maps, but instead of the map I have a background picture and instead of the markers I have other images (the cars).
Now for the implementation...
I have a simple ScrollableView which contains an image view. I am trying to overlay other multiple pictures that I want to keep placement but not zoom in or out (or at least not as much as the main picture). I would say that it is similar to a map which has location pins on it, but it is just using pictures.
I am doing this in Swift and the latest iOS version (9x)
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate
{
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var pin1View: UIImageView!
#IBOutlet weak var pin2View: UIImageView!
override func viewDidLoad()
{
super.viewDidLoad()
self.scrollView.minimumZoomScale = 1.0;
self.scrollView.maximumZoomScale = 10.0;
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?
{
let pin1_X = self.pin1View.frame.origin.x
let pin2_Y = self.pin2View.frame.origin.y
//I guess I need to do something here but not sure
return self.imageView
}
}
Any tips much appreciated. I googled it but couldn't find examples and still learning views and swift as well as general mobile dev.
Thanks for your help.
You have the change centre of the pin accordingly. Use this updated code:
class ViewController: UIViewController, UIScrollViewDelegate
{
#IBOutlet weak var scrollView: UIScrollView!
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var pin1View: UIImageView!
#IBOutlet weak var pin2View: UIImageView!
var pin1ViewCenter : CGPoint = CGPointZero
var pin2ViewCenter : CGPoint = CGPointZero
override func viewDidLoad()
{
super.viewDidLoad()
self.scrollView.scrollEnabled = true
self.scrollView.minimumZoomScale = 1.0
self.scrollView.maximumZoomScale = 10.0
pin1ViewCenter = pin1View.center
pin2ViewCenter = pin2View.center
self.scrollView.contentSize = self.imageView.frame.size
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView?
{
return self.imageView
}
func scrollViewDidZoom(scrollView: UIScrollView) {
let scaleAffineTransform = CGAffineTransformScale(CGAffineTransformIdentity, scrollView.zoomScale, scrollView.zoomScale)
var translatedPoint = CGPointApplyAffineTransform(pin1ViewCenter, scaleAffineTransform)
pin1View.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, translatedPoint.x - pin1ViewCenter.x , translatedPoint.y - pin1ViewCenter.y)
translatedPoint = CGPointApplyAffineTransform(pin2ViewCenter, scaleAffineTransform)
pin2View.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, translatedPoint.x - pin2ViewCenter.x, translatedPoint.y - pin2ViewCenter.y)
}
func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
let scaleAffineTransform = CGAffineTransformScale(CGAffineTransformIdentity, scale, scale)
scrollView.contentSize = CGSizeApplyAffineTransform(self.imageView.bounds.size, scaleAffineTransform)
}
}

Linking text views to scroll together

I have two side by side text views. How can I link them so that if one is scrolled down other automatically scrolls the same as first one in swift 2.
Set your viewController conforms to UITextViewDelegate,then set textView delegate to self,then in scrollViewDidScroll sync both contentOffSet
For example
Gif
Code
class ViewController: UIViewController,UITextViewDelegate {
#IBOutlet weak var textview2: UITextView!
#IBOutlet weak var textview1: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textview1.delegate = self
textview2.delegate = self
// Do any additional setup after loading the view, typically from a nib.
}
func scrollViewDidScroll(scrollView: UIScrollView) {
if scrollView == textview1{
textview2.contentOffset = textview1.contentOffset
}else{
textview1.contentOffset = textview2.contentOffset
}
}
}

Resources