What control to use to capture image from camera and show on ui as thumbnail with events xamarin ios - swift

I have created a view like this in my xamarin iOS mobile project.
User will be able to click on the capture images button to take a picture and set the image property of the image view. I would like to know how can i allow the user longpress a image (after it has been captured) and popup a message box to delete the image.
I have tried what Sameer has suggested in his comment like so in my ViewDidLoad
var gestureRecognizer = new UILongPressGestureRecognizer();
gestureRecognizer.AddTarget(() => ButtonLongPressed(gestureRecognizer));
img1.AddGestureRecognizer(gestureRecognizer);
img2.AddGestureRecognizer(gestureRecognizer);
When i click and hold on the image nothing happens. I have added these image views via the designer.
After a little more research and using the comment from #Junior Jiang - MSFT. I have made a bit progress but i would like to know which UIImage view has been clicked.
Heres my current code:
public JobImagesViewController(Job passedInCurrentJob) : base("JobImagesViewController", null)
{
currentJob = passedInCurrentJob;
uIImageViews = new List<UIImageView>();
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
uIImageViews.Add(img1);
uIImageViews.Add(img2);
uIImageViews.Add(img3);
uIImageViews.Add(img4);
uIImageViews.Add(img5);
uIImageViews.Add(img6);
uIImageViews.Add(img7);
uIImageViews.Add(img8);
uIImageViews.Add(img9);
uIImageViews.Add(img10);
InitialiseImageGrid();
// Add a Save button to the navigation bar
this.NavigationItem.SetRightBarButtonItem(
new UIBarButtonItem("Save", UIBarButtonItemStyle.Done, (sender, args) =>
{
//TODO if else block with all logic to check if there are images etc.
//TODO prompt user and ask if they would like to save images.
UIAlertView alert = new UIAlertView("Save Images?", "Save images against the Job?", null, "Yes", new string[] { "No" });
alert.Clicked += (s, e) =>
{
if (e.ButtonIndex == 0) // Yes clicked
{
SaveJobImages();
}
};
alert.Show();
}), true);
}
[Export("ImageLongPressed:")]
public void ImageLongPressed(UILongPressGestureRecognizer gestureRecognizer)
{
if (gestureRecognizer.State != UIGestureRecognizerState.Began)
{
return;
// Needed because selector is executed twice, because Long-press gestures are continuous
}
// Perform action of opening the dialog to select/take a picture, replacing the ? image with the new image
UIAlertView alert = new UIAlertView("Delete this image ?" , "Are you sure you want to delete this image?", null, "Yes", new string[] { "No" });
alert.Clicked += (s, e) =>
{
if (e.ButtonIndex == 0) // 'Accept' clicked
{
// TODO how to get the image which has been clicked??
}
};
alert.Show();
}
private void InitialiseImageGrid()
{
_imageList = DataAccess.GetImages(currentJob.jobAddressID);
var imageBytes = _imageList.Select(x => x.ImageBytes).ToList();
var gestureRecognizer = new UILongPressGestureRecognizer(this, new ObjCRuntime.Selector("ImageLongPressed:"));
gestureRecognizer.AddTarget(() => ImageLongPressed(gestureRecognizer));
// Populate the image views.
// TODO need to find a way to assign it to every imageview on the view without looping maybe linq???
int i = 0;
foreach (var item in imageBytes)
{
var imagedata = NSData.FromArray(item);
var img = UIImage.LoadFromData(imagedata);
if (uIImageViews != null && uIImageViews.Count > i)
{
uIImageViews[i].UserInteractionEnabled = true;
uIImageViews[i].AddGestureRecognizer(gestureRecognizer);
uIImageViews[i].AddGestureRecognizer(gestureRecognizer);
uIImageViews[i].Image = img;
}
i++;
}

It depends on how you are creating your view, are you using a XIB with all the images on there as buttons with images, and IBActions connected to those buttons. just make the sender a UILongPressGestureRecognizer.
If you are doing it through code, then in your ViewDidLoad you want to set your buttons to have an initial ? (or Image Needed) background image and then you add the UILongPress GestureRecognizer to each on of them. So if you had one button, you would do this:
public override void ViewDidLoad ()
{
// Perform any additional setup after loading the view
UIButton button = new UIButton (new System.Drawing.RectangleF(100, 100, 100, 30));
button.SetBackgroundImage ("imageNeeded", UIControlState.Normal);
var gestureRecognizer = new UILongPressGestureRecognizer ();
gestureRecognizer.AddTarget(() => this.ButtonLongPressed(gestureRecognizer));
button.AddGestureRecognizer(gestureRecognizer);
this.View.Add (button);
}
public void ButtonLongPressed(UILongPressGestureRecognizer gestureRecognizer)
{
if (gestureRecognizer.State != UIGestureRecognizerState.Began)
{
return;
// Needed because selector is executed twice, because Long-press gestures are continuous
}
// Perform action of opening the dialog to select/take a picture, replacing the ? image with the new image
}

If want to use UILongPressGestureRecognizer in UIImage, should set ImageView's UserInteractionEnabled be true.
As follow:
List<UIImageView> imageViews = new List<UIImageView>();
UIImage image = UIImage.FromFile("off.png");
ImageViewOne.Image = image;
ImageViewOne.Tag = 1;
ImageViewTwo.Image = image;
ImageViewTwo.Tag = 2;
imageViews.Add(ImageViewOne);
imageViews.Add(ImageViewTwo);
foreach (var imageview in imageViews)
{
imageview.UserInteractionEnabled = true;
UILongPressGestureRecognizer uITapGestureRecognizer = new UILongPressGestureRecognizer();
uITapGestureRecognizer.AddTarget(() => { Console.WriteLine("You choose View " + imageview.Tag); });
imageview.AddGestureRecognizer(uITapGestureRecognizer);
}
Here is official document from Apple.

Related

Can I put a static image in a UIButton.imageView? that is animating?

I have a custom UIButton (MovesButton) with an image that I don't want to change or remove. But when I set button.imageView?.animationImages with bunch of images and start animating it, it removes my button's initial photo I have. If I have the static photo as a background, it looks like it is on fire. I want the fire to be as a background and preferably in the same UIButton class.
private func putAnimation(button: MovesButton) {
var images: [UIImage] = []
if button.animation == ButtonAnimations.None {
print("None11")
return
} else if button.animation == ButtonAnimations.SmallFire {
for i in 0 ... ButtonAnimations.SmallFire.1 {
images.append(UIImage(named: "\(ButtonAnimations.SmallFire.0)\(i)")!) //ButtonAnimations.SmallFire.0 = "smallFire"
}
} else if button.animation == ButtonAnimations.BigFire {
for i in 0 ... ButtonAnimations.BigFire.1 {
images.append(UIImage(named: "\(ButtonAnimations.BigFire.0)\(i)")!)
}
} else { print("weird button animations") }
button.imageView?.animationImages = images
button.imageView?.animationDuration = 1
button.imageView?.startAnimating()
}
I don't want to use a hacky trick of putting an imageView or another button behind MovesButton
I want the same animation result as a button.imageView.animationImages but as the backgroundImage. Is this possible?
Instead of image view animation, use image animation. Call UIImage animatedImage(with:duration:) to form an animated image. Set that as your button’s background image and you’re all set.

how to enable button when button already changed in swift

so i have 3 buttons.
the first one for take a picture/pick image from gallery
the second one for take an autograph and segue it to another view controller
the last one is the next button
i want to disable my next button if the user have not full fill the image and the signature.And enable the button if user already full fill the image and the signature
Probably not the best solution but this should work:
button3.isEnabled = false
var signature: String = "" {
didSet {
button3.isEnabled = (image != nil && signature != "")
}
}
var image: UIImage? {
didSet {
button3.isEnabled = (image != nil && signature != "")
}
}

Scale button text to button size (not vice versa)

I have a simple alert shown below that is displayed differently in Windows and Linux. There are quite a bit of posts to scale button size depending on text but I essentially want the opposite. The Buttons should stay the same size and the text should scale to fit (like it appears to be doing in windows)
protected static void setDifficulty(){
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Welcome to the game of Memory");
alert.setHeaderText("How difficult should your oppenent be?");
alert.setContentText("Please choose your desired difficulty.");
ButtonType button1 = new ButtonType("Nice n' Easy");
ButtonType button2 = new ButtonType("So So");
ButtonType button3 = new ButtonType("Super Freak");
ButtonType buttonCancel = new ButtonType("Cancel", ButtonData.CANCEL_CLOSE);
alert.getButtonTypes().setAll(button1, button2, button3, buttonCancel);
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == button1){
MemoryField.difficulty = 10;
} else if (result.get() == button2) {
MemoryField.difficulty = 5;
} else if (result.get() == button3) {
MemoryField.difficulty = 0;
} else {
Platform.exit();
}
}
On Windows:
On Ubuntu:
The only way I was able to avoid this problem on Linux was to get a handle to the button, and set the preferred width:
for(ButtonType buttonType : alert.getButtonTypes()){
Button button = (Button) alert.getDialogPane().lookupButton(buttonType);
button.setPrefWidth(100);
}

While editing the content of webview , scrollview won't move upwards and keyboard hides the lines

I am developing an app as like rich editor text and as i have wrote the text on editable web view but keyboard hides the lines. I am also using some javascript coding but not succeeded.
this.updateOffset = function() {
try{
var sel = window.getSelection();
range = sel.getRangeAt(0);
if(this.tmpSpan==null){
this.tmpSpan = document.createElement('span');
}
range.insertNode(this.tmpSpan);
this.yOffset = this.tmpSpan.offsetTop;
this.xOffset = this.tmpSpan.offsetLeft;
this.tmpSpan.parentNode.removeChild(this.tmpSpan);
}
catch(exc){
log('updateOffset:' + exc.toString());
}
}
// eContent is the div with 'contenteditable', while visibleHeight is an int, set from objective-c (depending on where the webview is positioned, keyboard height and screen height)
this.scrollToVisible = function(){
try {
if(this.eContent.clientHeight>this.visibleHeight){
this.updateOffset();
if(this.yOffset<window.pageYOffset){
window.scrollTo(0, this.yOffset);
}
else if(this.yOffset-window.pageYOffset>this.visibleHeight){
window.scrollTo(0, this.yOffset-this.visibleHeight);
}
}
}
catch (exc){
log('scrollToVisible: ', exc.toString());
}
}
Please suggest what we should do.

How to hide/show view in Titanium?

I am new to Titanium.
I have taken 3 view and want to hide show that view on button click for iPhone app.
Any idea how to achieve this?
You can hide / show a view very easily, heres a self contained example:
var win = Ti.UI.createWindow();
var view = Ti.UI.createView({
width : 100,
height : 100,
backgroundColor : 'red'
});
var redView = Ti.UI.createView({
title : 'Hide / Show Red View',
bottom : 0,
width : 200,
height : 35
});
var visible = true;
button.addEventListener('click', function(e) {
if(visible) {
redView.hide();
} else {
redView.show();
}
visible = !visible;
});
win.add(redView);
win.add(button);
win.open();
While the other answer is certainly useful, two other (slightly) different methods for doing the same thing, just in case Titanium flips out when you use show() and hide().
//Method 1, using part of Josiah Hester's code snippet
var visible = true;
button.addEventListener('click', function(e) {
if(visible) {
redView.setVisible(false); //This is the exact same thing as hide() method
} else {
redView.setVisible(true); //This is the exact same thing as show() method
}
visible = !visible;
});
You can set opacity to 0 and even if the view's visible property is set to true, it will still be invisible, due to a completely nonexistent level of opacity. This is useful if you want something to be visible, but not clickable (by placing a view behind a view with an opacity of zero.
//Method 2, same code section
var opacity = 0;
button.addEventListener('click', function(e) {
if(opacity) {
redView.setOpacity(0); //This is the NOT the same thing as hide() method
} else {
redView.setOpacity(1); //This is the NOT thesame thing as show() method
}
opacity = Math.abs(opacity - 1);
});