Shake Animation for UIAlertView in swfit - animation

I know that there are some similar question on this for Objective-C. Does anybody know how to make it in Swift? Below is my alertView function.
func alertError(errorString: String?){
let alertController = UIAlertController(title: "Error Detected", message:
errorString, preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,
handler: nil))
self.presentViewController(alertController, animated: true, completion: nil)
}

Looking at this thread and converting the code in the accepted answer, here is what I got:
var dialog: UIAlertView = UIAlertView(title: "Title", message: "Message:", delegate: self, cancelButtonTitle: "Cancel", otherButtonTitles: "Done", nil)
dialog.alertViewStyle = UIAlertViewStylePlainTextInput
dialog.show()
var dillusionView: UIView = UIView(frame: CGRectMake(0, 0, view.frame.size.width, view.frame.size.height))
dillusionView.backgroundColor = UIColor.blackColor()
view.addSubview(dillusionView)
UIView(duration: 1.0, delay: 0.0, options: UIViewKeyframeAnimationOptionAutoreverse | UIViewKeyframeAnimationOptionRepeat, animations: { UIView(relativeStartTime: 0.0, relativeDuration: 0.5, animations: { dialog.window.transform = CGAffineTransformTranslate(dialog.transform, dialog.frame.origin.x - 10, dialog.frame.origin.y)
})
UIView(relativeStartTime: 0.5, relativeDuration: 0.5, animations: { dialog.window.transform = CGAffineTransformTranslate(dialog.transform, dialog.frame.origin.x + 10, dialog.frame.origin.y)
})
}, completion: nil)
As a warning there could be some bugs in this code. Hope it helps!

Related

Creating an Image in UIAlertController

can anyone please tell me a way of adding in an image into a UIAlertController? This is the code I have generated to produce the Alert Controller.
I believe I need to add a subview but not sure?
let myAlert = UIAlertController(title: "Congratualtions", message: "Correct answer selected, move on to next level", preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.default)
{
action in self.dismiss(animated: true, completion: nil)
}
myAlert.addAction(okAction);
self.present(myAlert, animated: true, completion:nil)
The code works but i do not know how add an image into the action sheet...
You can use this library (MZFormSheetPresentationController) to create the AlertViews with Images,Buttons and lots of functions. You can design viewController as you want...
func formSheetControllerWithNavigationController() -> UINavigationController {
return self.storyboard!.instantiateViewController(withIdentifier: "formSheetController") as! UINavigationController
}
func passDataToViewControllerAction() {
let navigationController = self.formSheetControllerWithNavigationController()
let formSheetController = MZFormSheetPresentationViewController(contentViewController: navigationController)
formSheetController.presentationController?.isTransparentTouchEnabled = false
formSheetController.presentationController?.shouldCenterVertically = true
formSheetController.presentationController?.shouldCenterHorizontally = true
formSheetController.presentationController?.shouldDismissOnBackgroundViewTap = true
let yourViewController = navigationController.viewControllers.first as! EventDisplayViewController
yourViewController.delegate = self
self.present(formSheetController, animated: true, completion: nil)
}

Unable to dismissViewControllerAnimated iOS 9.3

I am showing an UIAlertController and in UIAlertAction I am dismissing view controller. It was working fine prior to iOS 9.3. But in iOS 9.3 it is not working. Following is the code.
let alertController = UIAlertController(title: "Logged In Successfully", message: "asda", preferredStyle: .Alert)
// Create the actions
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { UIAlertAction in
self.dismissViewControllerAnimated(true, completion: nil)
}
// Add the actions
alertController.addAction(okAction)
// Present the controller
self.presentViewController(alertController, animated: true, completion: nil)
Try to put breakpoint in the dismissViewControllerAnimated line, and check if it's getting there when you press OK.
If it's not - try to replace
UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { UIAlertAction in
self.dismissViewControllerAnimated(true, completion: nil)
}
with this:
UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { _ in
self.dismissViewControllerAnimated(true, completion: nil)
}

Shared Text doesn't get shared to Facebook (but to others)

I have this code that shares a screenshot of the app, an initial Text and a URL via the Apple Share menu when a Share Button is pressed. It works like a charm with Twitter, Messages, Email etc, but when i wanna share to Facebook or the Facebook Messenger, the text doesn't show up (but the image and the URL do).
Any idea why that is and how to fix it?
I was able to share text and a URL to Facebook with other functions (but they didn't deliver the right screenshot so i switched to this one).
Thanks in advance!
Edit: The text does get shared in the xcode Simulator, just not on the iPhone(s).
func takeSnapshot(view: UIView) {
let bounds = UIScreen.mainScreen().bounds
UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0)
self.view.drawViewHierarchyInRect(bounds, afterScreenUpdates: false)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let text = "Text to be shared... #Hashtag"
let URL = NSURL(string: "http://stackoverflow.com/")!
let activityViewController = UIActivityViewController(activityItems: [image, text, URL], applicationActivities: nil)
self.presentViewController(activityViewController, animated: true, completion: nil)
}
An update to the Facebook application has replaced the in-built Facebook share activity with one that ignores status text - If you remove the Facebook app from your device the text will be displayed using the code you have. If you have the Facebook app installed you get images, URLs but not the text.
Follow the instructions: https://developers.facebook.com/docs/sharing/ios
Then you can do something like this:
Share a link
let content : FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: "<INSERT STRING HERE>")
content.contentTitle = "<INSERT STRING HERE>"
content.contentDescription = "<INSERT STRING HERE>"
content.imageURL = NSURL(string: "<INSERT STRING HERE>")
let button : FBSDKShareButton = FBSDKShareButton()
button.shareContent = content
button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 100) * 0.5, 50, 100, 25)
self.view.addSubview(button)
Share Photos
class ViewController: UIViewController, FBSDKLoginButtonDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate
let button : UIButton = UIButton()
button.backgroundColor = UIColor.blueColor()
button.setTitle("Choose Photo", forState: .Normal)
button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 150) * 0.5, 125, 150, 25)
button.addTarget(self, action: "photoBtnClicked", forControlEvents: .TouchUpInside)
self.view.addSubview(button)
let label : UILabel = UILabel()
label.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 200) * 0.5, 100, 200, 25)
label.text = "Photos Example"
label.textAlignment = .Center
self.view.addSubview(label)
func photoBtnClicked(){
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
println("Photo capture")
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum;
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let photo : FBSDKSharePhoto = FBSDKSharePhoto()
photo.image = info[UIImagePickerControllerOriginalImage] as! UIImage
photo.userGenerated = true
let content : FBSDKSharePhotoContent = FBSDKSharePhotoContent()
content.photos = [photo]
}
Photos must be less than 12 MB in size
Share Video on Facebook
class ViewController: UIViewController, FBSDKLoginButtonDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate
let button : UIButton = UIButton()
button.backgroundColor = UIColor.blueColor()
button.setTitle("Choose Video", forState: .Normal)
button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 150) * 0.5, 200, 150, 25)
button.addTarget(self, action: "videoBtnClicked", forControlEvents: .TouchUpInside)
self.view.addSubview(button)
let label : UILabel = UILabel()
label.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 200) * 0.5, 175, 200, 25)
label.text = "Video Example"
label.textAlignment = .Center
self.view.addSubview(label)
func videoBtnClicked(){
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSource Type.SavedPhotosAlbum){
println("Video capture")
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let video : FBSDKShareVideo = FBSDKShareVideo()
video.videoURL = info[UIImagePickerControllerMediaURL] as! NSURL
let content : FBSDKShareVideoContent = FBSDKShareVideoContent()
content.video = video
}
Videos must be less than 12MB in size. I hope I have helped you. (Sorry for my english)

Accessing picture library in an ActionSheet style

I'm trying to create a profilePic in a Sign Up View Controller. The profilePic is of type UIImageView. I want to be able to tap it so it instantly pulls up a photo library in an ActionSheet Style. And to also have one of the image box a button to access the camera. Is this possible?
import UIKit
class SignUpViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet weak var profilePic: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// PROFILE PICTURE
let tapGesture = UITapGestureRecognizer(target: self, action: "imageTapped:")
profilePic.addGestureRecognizer(tapGesture)
profilePic.userInteractionEnabled = true
}
func imageTapped(gesture:UIGestureRecognizer) {
if let profile1Pic = gesture.view as? UIImageView {
print("Image Tapped")
}
}
Try this code below :
import UIKit
class SignUpViewController: UIViewController, UIImagePickerControllerDelegate {
#IBOutlet weak var profilePic: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: "imageTapped:")
profilePic.addGestureRecognizer(tapGesture)
profilePic.userInteractionEnabled = true
}
func imageTapped(gesture:UIGestureRecognizer) {
if let profile1Pic = gesture.view as? UIImageView {
print("Image Tapped")
showActionSheet()
}
}
func camera()
{
var myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = UIImagePickerControllerSourceType.Camera
self.presentViewController(myPickerController, animated: true, completion: nil)
}
func photoLibrary()
{
var myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(myPickerController, animated: true, completion: nil)
}
func showActionSheet() {
let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
actionSheet.addAction(UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
self.camera()
}))
actionSheet.addAction(UIAlertAction(title: "Gallery", style: UIAlertActionStyle.Default, handler: { (alert:UIAlertAction!) -> Void in
self.photoLibrary()
}))
actionSheet.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
self.presentViewController(actionSheet, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
profilePic.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
}
}
Its working on my side..hope will do the some for you !
I recently published a GitHub repository to handle this type of Action Sheet.
You can download this class from here : MSActionSheet
and simply write :
EDIT :
● Now supporting iPad
MSActionSheet(viewController: self, sourceView: sender).showFullActionSheet {
sender.setImage($0, for: .normal)
}
MSActionSheet : is the best library for set user's profile picture.
I used this library but it's not supported in iPad so i
customise for some functions for support to iPad and Display with
popOverController.
Below the step of customisation.
Replace function with showFullActionSheet
func showFullActionSheet(on vc : UIViewController, over btn : UIButton,handler : #escaping (_ : UIImage?) -> Void) {
let sheet = create().addLibrary().addFrontCamera().addRearCamera().addCancelButton()
sheet.show(on: vc,over: btn){ (image : UIImage?) in
handler(image)
}
}
Add code before vc.present(MSActionSheet.sheet!, animated: true, completion: nil) in function show
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
if let presenter = MSActionSheet.sheet?.popoverPresentationController {
presenter.sourceView = btn
presenter.sourceRect = btn.bounds
}
}
Add below code before clientVC?.present(picker, animated: true, completion: nil) in function handler
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
picker.modalPresentationStyle = .popover
if let presenter = picker.popoverPresentationController {
presenter.sourceView = sourceBtn
presenter.sourceRect = sourceBtn!.bounds
}
}
How to use
let msActionSheet = MSActionSheet.instance
msActionSheet.showFullActionSheet(on: self,over: btn){ (image) in
btn.setImage(image, for: .normal)
}
msActionSheet.tintColor(color: Constants.kColorBluish)
Swift 3x:
class yourClassName:UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
#IBAction func yourButtonName(_ sender: UIButton) {
let pickerView = UIImagePickerController()
pickerView.delegate = self
pickerView.allowsEditing = true
let actionSheetControllerIOS8: UIAlertController = UIAlertController(title: "Please select", message: nil, preferredStyle: .actionSheet)
let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
print("Cancel") //Cancel
}
actionSheetControllerIOS8.addAction(cancelActionButton)
let saveActionButton: UIAlertAction = UIAlertAction(title: "Take Photo", style: .default) { action -> Void in
print("Take Photo") //Will open Camera
if UIImagePickerController.isSourceTypeAvailable(.camera) {
pickerView.sourceType = .camera
self.present(pickerView, animated: true, completion: { _ in })
}
}
actionSheetControllerIOS8.addAction(saveActionButton)
let deleteActionButton: UIAlertAction = UIAlertAction(title: "Camera Roll", style: .default) { action -> Void in
print("Camera Roll") //Will open Gallery
pickerView.sourceType = .photoLibrary
self.present(pickerView, animated: true, completion: { _ in })
}
actionSheetControllerIOS8.addAction(deleteActionButton)
let deleteActionButton2: UIAlertAction = UIAlertAction(title: "Import from Facebook", style: .default) { action -> Void in
print("Import from Facebook")
}
actionSheetControllerIOS8.addAction(deleteActionButton2)
self.present(actionSheetControllerIOS8, animated: true, completion: nil)
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//print(info)
if let pickedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
//print(pickedImage)
yourImageView.contentMode = .scaleAspectFit
yourImageView.image = pickedImage
}
dismiss(animated: true, completion: nil)
}
}

Add image into UIAlertController in swift

I want to have a simple UIAlertController with image in it. what should I do?
let alertMessage = UIAlertController(title: "Service Unavailable", message: "Please retry later", preferredStyle: .Alert)
alertMessage.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
self.presentViewController(alertMessage, animated: true, completion: nil)
Try this:
let alertMessage = UIAlertController(title: "Service Unavailable", message: "Please retry later", preferredStyle: .Alert)
let image = UIImage(named: "myImage")
var action = UIAlertAction(title: "OK", style: .Default, handler: nil)
action.setValue(image, forKey: "image")
alertMessage .addAction(action)
self.presentViewController(alertMessage, animated: true, completion: nil)
a snippet of code that works for me:
func alert2 (heading: String,image: String){
if self.presentedViewController == nil {
let alertController = UIAlertController(title: nil, message: " ", preferredStyle: .Alert )
var imageView = UIImageView(frame: CGRectMake(10, 6, 300, 50))
imageView.image = UIImage(named: display.0)
alertController.view.addSubview(imageView)
alertController.addAction(UIAlertAction(title: "Maps", style: UIAlertActionStyle.Default, handler: {(action:UIAlertAction!) in self.makeContact("maps")}))
this is my code to image in alert view
var logoAlert = UIImageView(frame: CGRectMake(0, 0, 300, 250))
logoAlert.image = UIImage(named: "v1.png")
infoController.view.addSubview(logoAlert)
NSLog("image is draw")
logoalert is dtring
infoController - is name of fun
rename it, and all will be alright
paste this code in fun of alert controller

Resources