UIButton in code - ios

I need to add a gesture (move, rotate, scale) to a UIButton.
I created the button in code, but I don't know how to use it.
If the button is touched I want to move, scale or rotate.
Here is my code:
-(void)addButton{
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(self.view.bounds.size.width,1000.0);
buttonImage1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonImage1.frame = CGRectMake(100, 20, 100, 100);
UIImage *btnImage1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:adres1]]];
[buttonImage1 setImage:btnImage1 forState:UIControlStateNormal];
[scrollview addSubview:buttonImage1];
buttonImage2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonImage2.frame = CGRectMake(100, 180, 150, 150);
UIImage *btnImage2 = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:adres2]]];
[buttonImage2 setImage:btnImage2 forState:UIControlStateNormal];
[scrollview addSubview:buttonImage2];
buttonImage3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
buttonImage3.frame = CGRectMake(100, 450, 194, 146);
UIImage *btnImage3 = [UIImage imageWithData:[NSData dataWithContentsOfURL:
[NSURL URLWithString:adres3]]];
[buttonImage3 setImage:btnImage3 forState:UIControlStateNormal];
[scrollview addSubview:buttonImage3];
}
-(void) move
{
}

I need to add a gesture (move, rotate, scale) to a UIButton.
I'm not sure I understand what you mean about adding a gesture. Right now, your buttons aren't set up to do anything at all.
I created the button in code, but I don't know how to use it.
You need to add a target and action to your button. When a button is tapped, it sends an action message to some other object, which is its target. To set up a button, you need to add a target and action using the -addTarget:action:forControlEvents: method inherited from UIControl. If you want the button to send a move message to the same object that's creating the button, you'd change your addButton method to include this:
[buttonImage1 addTarget:self action:#selector(move) forControlEvents:UIControlEventTouchUpInside];

Related

How to identify the subviews (imageviews) addeded to the UIButton in UIAutomation?

I have a UIButton and the button background is set to an image. Added
an image view on the top right corner of the button and I set it
to different image when the button is clicked to show the selection.
When I try to automate this using instruments. I don’t see any subviews
(image views) in the UIAutomator.logElementTree()method does not show
any image views.
How to identify the image views added as subview in the UIButton?
Here is the code.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(40, 40, 100, 100);
[button addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[UIImage imageNamed:#“testImage.png”] forState:UIControlStateNormal];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
imageview.frame = CGRectMake(70,30,20,20);
[button addSubview:imageview];
You have to set the accessibilityEnabled & accessibilityLabel.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(40, 40, 100, 100);
[button addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
button.accessibilityEnabled = YES;
button.accessibilityLabel =#"My Button";
[button setBackgroundImage:[UIImage imageNamed:#“testImage.png”] forState:UIControlStateNormal];
UIImageView *imageview = [[UIImageView alloc] initWithImage:image];
imageview.accessibilityEnabled = YES;
imageview.accessibilityLabel = #"My Image";
imageview.frame = CGRectMake(70,30,20,20);
[button addSubview:imageview];
Refer this tutorial
Sweet Angel's answer is enough if you have a single UIImageView subview. However, I would suggest using tags to identify subviews.
imageView.tag = 1346;//Any arbitrary number (I am unsure if there are any limitations to the values you can use, but all 4 digit numbers work fine for me).
Then to get the imageView:
UIImageView* imageView = (UIImageView*)[button viewWithTag:1346];
That's it.
If you want to identify imageview on button taped, try following code :
- (IBAction)didTapButton:(UIButton *)sender
{
for (id view in sender.subviews)
{
if ([view isKindOfClass:[UIImageView class]])
{
NSLog(#"Image view captured.....");
}
}
}

Inactive part of the screen on fullscreen

I'm trying to put a picture on fullscreen with buttons on to do actions.
Here is a screenshot of my result:
My problem is that the buttons on the first line (the highest) are not touchable.
It seems to have no action on the top layout.
Here is my code:
- (void)showActionsForGlobalFullScreen:(UIGestureRecognizer *)gestureRecognizer {
//LEFT
UIImage* cancelImage = [UIImage imageNamed:#"cancel.png"];
CGRect frameimg = CGRectMake(3, 3, cancelImage.size.width, cancelImage.size.height);
UIButton *cancelButton = [[UIButton alloc] initWithFrame:frameimg];
UIImage* flagImage = [UIImage imageNamed:#"flag.png"];
CGRect frameimg2 = CGRectMake(3, 40, flagImage.size.width, flagImage.size.height);
UIButton *flagButton = [[UIButton alloc] initWithFrame:frameimg2];
[cancelButton addTarget:self action:#selector(closeFullView:) forControlEvents:UIControlEventAllTouchEvents];
[cancelButton setBackgroundImage:cancelImage forState:UIControlStateNormal];
[flagButton addTarget:self action:#selector(displayFlag:) forControlEvents:UIControlEventAllTouchEvents];
[flagButton setBackgroundImage:flagImage forState:UIControlStateNormal];
//RIGHT
UIImage *exitFullScreenImage = [UIImage imageNamed:#"exitFullScreen.png"];
CGRect frameimg3 = CGRectMake(fullScreenViewFromVC.frame.size.width - 25 , 3, exitFullScreenImage.size.width, exitFullScreenImage.size.height);
UIButton *exitFullScreenButton = [[UIButton alloc] initWithFrame:frameimg3];
UIImage *commentImage = [UIImage imageNamed:#"comment.png"];
CGRect frameimg4 = CGRectMake(fullScreenViewFromVC.frame.size.width - 25 , 40, commentImage.size.width, commentImage.size.height);
UIButton *commentButton = [[UIButton alloc] initWithFrame:frameimg4];
[exitFullScreenButton addTarget:self action:#selector(exitFullScreenPhoto:) forControlEvents:UIControlEventAllTouchEvents];
[exitFullScreenButton setBackgroundImage:exitFullScreenImage forState:UIControlStateNormal];
[commentButton addTarget:self action:#selector(diplayComments:) forControlEvents:UIControlEventAllTouchEvents];
[commentButton setBackgroundImage:commentImage forState:UIControlStateNormal];
//[actionsView addSubview:flagButton];
[fullScreenViewFromVC addSubview:cancelButton];
[fullScreenViewFromVC addSubview:flagButton];
[fullScreenViewFromVC addSubview:exitFullScreenButton];
[fullScreenViewFromVC addSubview:commentButton];
}
Important note:
fullScreenViewFromVC is an own class extending UIView and instantiated on my controller.
Basically I'm called the fullscreen method from a picture on a UITableViewCell
I'm using this fullScreenViewFromVC variable to store my data from the current selected cell to my controller.
Thank you for your help.
Finally it was because of another view that I had to hide.
I thought that changing de zPosition of my view in fullscreen should be enough but it did not.
Problem solved.

Getting white flash animation effect upon clicking play and pause buttons

I have a single button toggling between play and pause for audio playback. When I was using the standard play/pause buttons (e.g., initWithBarButtonSystemItem:UIBarButtonSystemItemPlay), I got a nice white flash animation effect over the buttons as they toggled back and forth. But now that I am using custom images, I no longer get that effect. How can I get it back?
Also would very much appreciate any tips to simplify this as it seems heavyweight. I saw https://stackoverflow.com/a/9104587/1462372 but am not clear whether that is applicable in this case (in button bar).
Here is the code:
- (void) setAsPlaying:(BOOL)isPlaying
{
self.rootViewController.playing = isPlaying;
// we need to change which of play/pause buttons are showing, if the one to
// reverse current action isn't showing
if ((isPlaying && !self.pauseButton) || (!isPlaying && !self.playButton))
{
UIBarButtonItem *buttonToRemove = nil;
UIBarButtonItem *buttonToAdd = nil;
if (isPlaying)
{
buttonToRemove = self.playButton;
self.playButton = nil;
UIImage *pauseButtonImage = [UIImage imageNamed:#"icon_pause.png"];
UIButton *pauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[pauseButton addTarget:self action:#selector(pauseAudio:) forControlEvents:UIControlEventTouchUpInside];
pauseButton.bounds = CGRectMake(0, 0, pauseButtonImage.size.width, pauseButtonImage.size.height);
[pauseButton setImage:pauseButtonImage forState:UIControlStateNormal];
self.pauseButton = [[UIBarButtonItem alloc] initWithCustomView:pauseButton];
buttonToAdd = self.pauseButton;
}
else
{
buttonToRemove = self.pauseButton;
self.pauseButton = nil;
// this was the way I was doing it before:
// self.playButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:#selector(playAudio:)];
UIImage *playButtonImage = [UIImage imageNamed:#"icon_play.png"];
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playButton addTarget:self action:#selector(playAudio:) forControlEvents:UIControlEventTouchUpInside];
playButton.bounds = CGRectMake(0, 0, playButtonImage.size.width, playButtonImage.size.height);
[playButton setImage:playButtonImage forState:UIControlStateNormal];
self.playButton = [[UIBarButtonItem alloc] initWithCustomView:playButton];
buttonToAdd = self.playButton;
}
// Get the reference to the current toolbar buttons
NSMutableArray *toolbarButtons = [[self.toolbar items] mutableCopy];
// Remove a button from the toolbar and add the other one
if (buttonToRemove)
[toolbarButtons removeObject:buttonToRemove];
if (![toolbarButtons containsObject:buttonToAdd])
[toolbarButtons insertObject:buttonToAdd atIndex:5];
[self.toolbar setItems:toolbarButtons];
}
}
Try this code might be helpful to you...
UIImage* image = [UIImage imageNamed:#"facebook.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height-10);
UIButton *rightbtnItem = [[UIButton alloc] initWithFrame:frame];
[rightbtnItem setBackgroundImage:image forState:UIControlStateNormal];
[rightbtnItem setShowsTouchWhenHighlighted:YES];
Copy and paste it in your viewDidload and see glow effect on touch.
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"ar.png"] forState:UIControlStateNormal];
[button setFrame:CGRectMake(0, 0, 30, 30)];
button.center=self.view.center;
[button setBackgroundImage:[UIImage imageNamed:#"RS2kQ.png"] forState:UIControlStateHighlighted];
Get Image from here ar.png and RS2kQ.png

How can i make clickable uiimage?

I have one menubar as a uiimage. I want to make 5 menu on this menubar are clickable and link to another view. Can anyone tell me the way i can do that? I found people using uibutton on the uiimage, Is that fine for me and how can i separate the menu bar to 5 pieces.
Thanks for both rmaddy and Brent Royal-Gordon
I decided to use the uiimage as a menubar background and add the uibutton on it. i try some thing like this:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"MOVIE" ofType:#"MP4"]];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.repeatMode = MPMovieRepeatModeOne;
moviePlayer.shouldAutoplay = YES;
moviePlayer.scalingMode = MPMovieScalingModeFill;
[moviePlayer.view setFrame:CGRectMake(0,0,1330,320)];
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setContentSize:CGSizeMake(1330,320)];
[scrollView setScrollEnabled: YES];
[scrollView setShowsHorizontalScrollIndicator:NO];
[scrollView setShowsVerticalScrollIndicator:NO];
[scrollView addSubview:moviePlayer.view];
[self.view addSubview:scrollView];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:image.frame];
UIImage *menubar = [UIImage imageNamed:#"BG.jpg"];
imageView = [[UIImageView alloc] initWithImage:menubar];
imageView.contentMode = UIViewContentModeScaleToFill;
imageView.frame = CGRectMake(0, 258, imageView.frame.size.width, 25);
[self.view addSubview:imageView];
[imageView setUserInteractionEnabled:YES];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0,258,30,25)];
[btn addTarget:self action:#selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"HOME" forState:UIControlStateNormal];
[imageView addSubview:btn];
but the button is not visible on screen when i run on the simulator. What did i do wrong?
Using a gesture recognizer is a bad idea because:
It forces you to interpret the touch areas for each submenu yourself.
It's opaque to VoiceOver.
It doesn't show a highlight when the user touches a button. These sorts of highlights help the user understand what the user interface is doing, so they're very valuable.
What you should do is split the single image into five in a graphics tool like Photoshop, and make each of these slices the image of a UIButton. You'll need to design highlighted states for these images (shown when one of them is held down), but you should be doing that anyway.
If you must slice the image in code, you can do that by using UIGraphicsBeginImageContextWithOptions(size, NO, 0.0) and drawing your big image using drawAtPoint: with an appropriate offset to get the slice you want, but 99.99999% of the time, it'll make more sense to slice the images once on your big, beefy, wall-plug-powered Mac than every time your app runs on a tiny, slow, battery-powered iOS device.
Use a UITapGestureRecognizer with the UIImageView containing your image. You will need to set the userInteractionEnabled property to YES on the image view.
When you get the tap event, look at where in the view the tap occurred. Based on the location, perform one of the five menu actions.
If you want more control, you may be better off splitting the image into 5 and creating 5 UIButton instances instead.
Why you add it the buttons to image view ? Just adding it to the View. It will solve the problem(as i understand).
[imageView addSubview:btn];
Try like this
[self.view addSubview:btn];
Hope this helps
issue is with the Width of the button.Try
this
[btn setFrame:CGRectMake(0,258,100,50)]
(increase width to 100)
and you may need to change the title colour if your background is white
[btn setTitleColor:[UIColor blackColor] forState:
UIControlStateNormal];
you Can first add button view then add imageview on the button please see bellow code :
//Add first button
UIButton *btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
btnImage.frame = CGRectMake(0, 4, 60, 56);
btnImage.backgroundColor = [UIColor colorWithRed:231.0f/255.0f green:232.0f/255.0f blue:234.0f/255.0f alpha:1.0];
btnImage.layer.borderColor = [UIColor colorWithRed:183.0f/255.0f green:184.0f/255.0f blue:186.0f/255.0f alpha:1.0].CGColor;
btnImage.layer.borderWidth = 1.0f;
// Add imageview on button
UIImageView *yourImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
[yourImageView setImage:[UIImage imageNamed:#"myimage.png"]];
yourImageView.center = CGPointMake(btnImage.frame.size.width/2, btnImage.frame.size.height/2);
[btnImage addSubview:yourImageView];
[self.view addSubview:btnImage];

Add text to a custom Back Button iOS

I have a backbutton with an image I'm using in my navigation controller, and I'd like to set the text over top of the image. I tried this but it doesn't seem to show up:
UIImage *normalBackImage = [UIImage imageNamed:#"button_tl.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom]; backButton.bounds = CGRectMake(0, 0, normalBackImage.size.width, normalBackImage.size.height );
[backButton setImage:normalBackImage forState:UIControlStateNormal];
backButton.titleLabel.text = #"Back";
Does anyone know how to accomplish this? Thanks!
set the title's color, else you'll not see anything :)
setTitleColor:forState:
2.
dont try to set the image (which means FOREGROUND image). Instead set the BACKGROUND image :)
– setBackgroundImage:forState:
sample:
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(10, 10, 100, 50);
[backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[backButton setTitle:#"Bla" forState:UIControlStateNormal];
UIImage *normalBackImage = [UIImage imageNamed:#"button_tl.png"];
[backButton setBackgroundImage:normalBackImage forState:UIControlStateNormal];
[self.view addSubview:backButton];
You could create a UILabel and add it as a subview to your UIImage. That would look something like this (writing code outside the compiler, might not be perfect)
UILabel *myLabel = [[UILabel alloc] init];
[myLabel setText:#"My Label Text"];
[image addSubview:myLabel];

Resources