Comparing uibutton clicks - iphone

I want to be able to control certain functions when a button is clicked either by having a bool or another variable know how many times the button was clicked. For example if the button was clicked once i want to make it display an NSLOG of lets say 1, if its pressed twice i want NSLOG of 2, however once you press it again I cant find a way to get it back to 1..

You could link it to a IBAction containing the following code:
- (IBAction) ButtonAction:(id)sender {
static int x = 0;
x++;
if (x == 3)
x = 1;
NSLog(#"%d", x);
}
And link the button "click" action (TouchUpInside) to this action in interface builder.
Hope it helps!

I don't understand exactly what you're looking for, but maybe something like this?
enum ButtonSequentialAction
{
kButtonSequentialAction1 = 0,
kButtonSequentialAction2,
kButtonSequentialAction3,
kButtonSequentialActionTotal
};
...
- (void) buttonPress
{
switch (m_CurrentButtonAction) // m_CurrentButtonAction is a member variable of the class
{
case kButtonSequentialAction1:
{
// do action 1;
break;
}
case kButtonSequentialAction2:
{
// do action 2;
break;
}
case kButtonSequentialAction3:
{
// do action 3;
break;
}
default:
{
// crap, shouldn't get here.
break;
}
}
m_CurrentButtonAction = (m_CurrentButtonAction + 1) % kButtonSequentialActionTotal;
}

Related

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

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.

Binding textfield text to enable button not working

I am new to reactive programming, and Bond framework specifically. I know I may be doing things that are wrong in my basic understanding of this programming technique. This is the situation:
I have a UITextView and an "approve" UIButton.
I want the approve button to be enabled only when the text in the textView is not nil. I have tried adding these lines of code into my viewDidLoad method in my ViewController.
textView.reactive.text.observeNext{(text) in
self.message = text
print(text)
}
textView.reactive.text.map { $0 != nil}.bind(to: approveButtonOutlet.reactive.isEnabled)
The first action works (printing the text is happening successfully on every input change).
The second one does not work, the button is enabled both when the text is not nil and when it is.
Any help appreciated.
You can try like
RAC(self.approveButtonOutlet, enabled) = [self.textView.rac_textSignal map:^id(NSString *text) {
return #(text.length > 0);
}];
I'm not sure how it will be in swift 3 just try like
RAC(self.approveButtonOutlet, enabled) = self.textView.rac_textSignal.map({(text: String) -> void in
return (text.length > 0)
})
I found the issue was that I had a placeholder in my textView, which prevented from the text really being nil. So what eventually I did is this:
textView.reactive.text.map {
if $0 == placeholder {
return false
} else if $0 != nil {
return $0!.characters.count > 0
} else{
return false
}
}.bind(to: approveButtonOutlet.reactive.isEnabled)

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);
}

Hiding back images in iCarouselTypeRotary view for iCarousel

I have set images in iCarousel. When I scroll the carousel, it shows images in front and back. I do not want to display images in back. Please help. Thank you.
You should implement the delegate:
- (CGFloat)carousel:(iCarousel *)_carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value{
switch (option)
{
case iCarouselOptionVisibleItems:
{
return numberOfItemsIntheFront;
}
}
}
You'll need to change to a custom carousel type, and copy the implementation of iCarouselTypeRotary in your delegate. Then implement -carousel:itemAlphaForOffset: so that items around the back have an alpha of zero.
In the latest version of iCarousel, the easiest way to do this is to set view.layer.doubleSided = NO on your carousel item views, or to use the iCarouselOptionShowBackfaces property, like this:
- (CGFloat)carousel:(iCarousel *)_carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
switch (option)
{
case iCarouselOptionShowBackfaces:
{
return NO;
}
default:
{
return value;
}
}
}
Code below will nicely fade away items that are going to the back. Enjoy.
func carousel(carousel: iCarousel, valueForOption option: iCarouselOption, withDefault value: CGFloat) -> CGFloat
{
if (option == .Spacing)
{
return value * 1.1
}
else if (option == .FadeMin)
{
return 0;
}
else if (option == .FadeMax)
{
return 0;
}
else if (option == .FadeRange)
{
return 3;
}
return value
}
I tried the solution suggested by #NickLockwood but doesn't seem to work :-( I added it to both methods in viewForItemAtIndex & placeholderViewAtIndex. My image size is 115*115 pixels and canvas size is 146*146 pixels. However, setting a return value of 'return value * 0.82f;' under iCarouselOptionSpacing (inside the method valueForOption) did the trick for me.
The easiest way to hide the background images
func carousel(_ carousel: iCarousel, valueFor option: iCarouselOption, withDefault value: CGFloat) -> CGFloat {
switch option {
case .spacing:
return 1.05
case .visibleItems:
return 3
default:
return value

How to cancel/reset an UIGestureRecognizer

how can I cancel or reset an UIGestureRecognizer? The problem is, that if I set waitForSomething to NO during a gesture, the next event is UIGestureRecognizerStateChanged. But the first event should be UIGestureRecognizerStateBegan.
My Code:
- (void) panned:(UIPanGestureRecognizer *) recognizer {
if (waitForSomething) {
// cancel or reset the recognizer!
// because the next event should be UIGestureRecognizerStateBegan and not UIGestureRecognizerStateChanged
return;
}
switch (recognizer.state) {
case UIGestureRecognizerStateBegan:
// important initialisation code
break;
case UIGestureRecognizerStateChanged:
// do something
break;
}
}
Thank you for you help!
I got it! :-)
Maybe someone else runs in this problem, here is the solution:
if (waitForSomething) {
recognizer.enabled = NO;
recognizer.enabled = YES;
return;
}
The next event will be UIGestureRecognizerStateFailed followed by UIGestureRecognizerStateBegan.
Swift 4:
if waitForSomething {
recognizer.isEnabled = false
recognizer.isEnabled = true
return
}
If you need to do this in more than one place:
in Swift
extension UIGestureRecognizer
{
func cancel() {
isEnabled = false
isEnabled = true
}
}