Show UIButton Done on UIScrollView for Images only when user taps on image view - iphone

How can i code for UIButton to show only when user taps on UIScrollView for images.
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 10, 60, 35);
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
Right now it is displaying on every image view but i want it should Show UIButton Done only when taps on the screen.
EDIT: If i add
[imageView addGestureRecognizer: tap];
and initiate Gesturerecognizer
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
[tap release];
then how can i code in handle tap method to show done button when user taps on image view
- (void)handleTap:(UIGestureRecognizer*)tap {
}
Thanks for help.

You need to add the gesture recognizer on the image view and not on the ViewController.
Also, you need to enable the user interaction for the image view like this:
imageView.userInteractionEnabled = YES;

Check UIScrollViewDelegate Method when you will scroll show your button will be shown and then if you want to hide that button hide it.
And for handleTap you set [imageView addGestureRecognizer:tap]; instead of [self addGestureRecognizer:tap];
create your button in ViewDidLoad and hide it initially and when user will tap image just set`
button.hidden = NO;

Related

Trouble Setting Button Image When On a ScrollView

I'm making an app. My app is based off this sample project. https://github.com/yeahdongcn/RSCircaPageControl
In the app there is a view. On the view, there is a scroll view (UIScrollView *scrollView) which allows to 10 "pages" of content. Within each page, there is another scroll view (UIScrollView *sv) which is a subview of *scrollView.
On SV, I then added a button called UIButton *button. When I attempt to change the button's images after a time of "two minutes," nothing happens. This is because there are 10 buttons. I need to change the image of the button on only one of the pages. Not all of them.
Something tells me it needs "object at index" or something to specify which button on which page I want to change.
The code is below! Thanks guys!
- (void)viewDidLoad
{
[super viewDidLoad];
self.clipView = [[RSView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
self.clipView.clipsToBounds = YES;
[self.view addSubview:self.clipView];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.clipView.bounds.size.width, kScrollViewHeight)];
self.scrollView.delegate = self;
self.scrollView.clipsToBounds = NO;
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.scrollView];
self.clipView.scrollView = self.scrollView;
self.pageControl = [[RSCircaPageControl alloc] initWithNumberOfPages:10];
CGRect frame = self.pageControl.frame;
frame.origin.x = self.view.bounds.size.width - frame.size.width - 10;
frame.origin.y = roundf((self.view.bounds.size.height - frame.size.height) / 2.);
self.pageControl.frame = frame;
self.pageControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
[self.pageControl setCurrentPage:0 usingScroller:NO];
[self.view addSubview:self.pageControl];
CGFloat currentY = 0;
for (int i = 0; i < 10; i++) {
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0, currentY, self.scrollView.bounds.size.width, kScrollViewHeight)];
sv.tag = kScrollViewTagBase + i;
sv.delegate = self;
sv.autoresizingMask = UIViewAutoresizingFlexibleWidth;
sv.backgroundColor = [UIColor colorWithRed:(arc4random() % 257) / 256. green:(arc4random() % 257) / 256. blue:(arc4random() % 257) / 256. alpha:1];
///here's the buttton!!
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage*normal = [UIImage imageNamed:#"girl.jpg"];
UIImage*selected = [UIImage imageNamed:#"girl2.jpg"];
button.frame = CGRectMake(100, 100, 50,50));
[button setImage:normal forState:UIControlStateNormal];
[button setImage:selected forState:UIControlStateHighlighted];
button.contentMode = UIViewContentModeScaleAspectFit;
[button addTarget:self
action:#selector(myButtonPressed:)
forControlEvents:UIControlEventTouchDown];
[sv addSubview:button]; //notice that the button is added as a subview of SV
[self.scrollView addSubview:sv]; // notice that SV is a subview of the scrollView.
if (i == 2 || i == 6) {
sv.contentSize = CGSizeMake(sv.contentSize.width, kScrollViewContentHeight);
}
currentY += kScrollViewHeight;
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, currentY);
[self performSelector:changeButtomImages withObject:nil afterDelay:TwoMinutes];
}
- (void)changeButtonImages {
UIImage*normal2 = [UIImage imageNamed:#"boy.jpg"];
UIImage*selected2 = [UIImage imageNamed:#"boy2.jpg"];
[button setImage:normal2 forState:UIControlStateNormal];
[button setImage:selected2 forState:UIControlStateHighlighted];
}
Kyle
for setting button image in a normal state the parameter to pass forState: is UIControlStateNormal not UIControlStateHighlighted
So, in changeButtonImages you are changing the the image for the iVar "button", but you are never actually assigning anything to that button var. In your for loop in viewDidLoad, you are creating a local variable of UIButton called button. Maybe something like this?
- (void)myButtonPressed:(id)sender
{
UIButton *button = (UIButton *)sender;
UIImage*normal2 = [UIImage imageNamed:#"boy.jpg"];
UIImage*selected2 = [UIImage imageNamed:#"boy2.jpg"];
[button setImage:normal2 forState:UIControlStateNormal];
[button setImage:selected2 forState:UIControlStateHighlighted];
}

UITapGestureRecognizer and UIButton

UITapGestureRecognizer and UIButton are not working together.
UIButton alone is working fine without UITapGesturerecognizer. It shows in all scrolling image views but after adding UITapGestureReconizer feature it is not showing UIButton when tapped.
BOOL numberofTaps;
#interface ImageScrollViewController : UIViewController <UIGestureRecognizerDelegate>
#property (nonatomic, assign) UITapGestureRecognizer *recognizer;
- (void)handleTap:(UIGestureRecognizer*)sender;
//////////////////////////
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 10, 60, 35);
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
myButton.hidden = YES;
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
recognizer.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:recognizer];
imageView.userInteractionEnabled = YES;
recognizer.delegate = self;
numberofTaps = 1;
[recognizer release];
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
// [imageScrollView addGestureRecognizer:tap];
// [imageView addSubview:tap];
[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
EDIT: Howcome this works but when i only uncomment myButton.hidden= NO then it works but still doesn't shows my button DONE on the imageviews
- (void)handleTap:(UIGestureRecognizer*)sender {
// if(numberofTaps == 1){
CGPoint tapPoint = [sender locationInView:_imageScrollView];
int tapX = (int) tapPoint.x;
int tapY = (int) tapPoint.y;
NSLog(#"TAPPED X:%d Y:%d", tapX, tapY);
//_myButton.hidden = NO;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hello" message:#"How are you?" delegate:nil cancelButtonTitle:#"I'm awesome." otherButtonTitles:nil];
[alert show];
[alert release];
}
What is the reason that otherwise it is working but not showing Done UIButton.
First you have to find button which you have to show. So for that you will have to setTag: on your UIButton before adding them to scrollView
like this -
myButton.tag = i;
Then add tag to your UIImageView also before adding them to scrollView like this -
imageView.tag = i*100;
Now in handleTap: method you can compare tags and can get the button which you have to show.
-(void)handleTap:(UIGestureRecognizer *)sender
{
//getting all buttons of scrollView
for(UIButton *button in scrollView.subviews)
{
//comparing tags
if(button.tag == sender.view.tag/100)
{
button.hidden = NO;
}
}
}
Do this, remove your if Condition.

Contentoffset for Tapped imageview in scrollview

I am trying to save the tapped image to a photo album, using contentOffset to detect which image object is tapped to save, but it always saves the last imageObject instead.
Here is how I try to calculate contentOffset for tapped image view in scrollView:
(void)viewDidLoad
{
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
imageScrollView.delegate = self;
imageScrollView.pagingEnabled = YES;
for (int i = 0; i < 61; i++) {
CGFloat xOrigin = i * 320;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 10, 60, 35);
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor clearColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
_imageView = [[[UIImageView alloc] initWithImage:image]autorelease];
_imageView.frame = CGRectMake(xOrigin, 0, 320, 480);
_imageView.tag = i;
[_imageScrollView viewWithTag:i+1];
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleLongPress:)];
imageScrollView.userInteractionEnabled = YES;
[imageScrollView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate = self;
[gestureRecognizer release];
[imageScrollView addSubview:_imageView];
[imageScrollView addSubview:myButton];
}
imageScrollView.contentSize = CGSizeMake(320 * 61 , 480);
[self.view addSubview:imageScrollView];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Save Photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
[actionSheet showInView:self.view];
[actionSheet release];
}}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
[self performSelector:#selector(LongPress:) withObject:nil];
break;
default:
break;
}}
- (void)LongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
CGPoint offset = _imageScrollView.contentOffset;
int imageView = floor((Offset.x - imageView / 320) / imageView) + 1;
UIImage* image = [(UIImageView*)[_imageScrollView viewWithTag:imageView] image];
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
}
I expected this code to save the tapped image, but it saves the last image view from the scrollview instead.
Please let me know if I am doing it incorrectly, and if I am still missing something. Thanks for the help.
The reason is in your viewDidLoad you are using
_image = [UIImage imageNamed:imageName];
I think this is a global variable in the class, and you are using this object to save the image to photo album. As it is a global variable in the last count of your loop's image will be pointing to it, so you are always getting last image saved.
You can fix it by making _image local within for loop. And as you are setting the tag, using the tag you can get the imageView/image from scrollView. You can have like this
//your imageView in longPress function has the selected imageView's tag
UIImage* selImage = [(UIImageView*)[imageScrollView viewWithTag:imageView] image];
This should work for you.
int imageView = floor((Offset.x - imageView / 320) / imageView) + 1;
By this line
int imageView = (int)(scrollView.contentOffset.x / scrollView.frame.size.width);

how add image with scroll view instead of uibutton in scrolling view?

In this code I want to do changes like as instead of button I want to create images in image view with scroll view with zoom in and zoom out event on image. And two or three button on every image view so that I can implement some event on button. How do that?
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 33;
[btnMenu setTag:0 ];
for (int i = 1; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
//awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
btnMenu = [UIButton buttonWithType:UIButtonTypeCustom];
//NSData *data =UIImageJPEGRepresentation(, 1);
[btnMenu setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"page-%d.jpg",i]] forState:UIControlStateNormal];
CGRect frame = btnMenu.frame;
frame.size.width=320;
frame.size.height=460;
frame.origin.x=0;
frame.origin.y=0;
btnMenu.frame=frame;
[btnMenu setTag:i];
btnMenu.alpha = 1;
[btnMenu addTarget:self action:#selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[awesomeView addSubview:btnMenu];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];
}
-(IBAction)btnSelected:(id)sender{
UIButton *button = (UIButton *)sender;
int whichButton = button.tag;
NSLog(#"Current TAG: %i", whichButton);
if(whichButton==1)
{
first=[[FirstImage alloc]init];
[self.navigationController pushViewController:first animated:YES];
}
}
You need Image instead of Button right? Then Try This:
scroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
scroll.backgroundColor=[UIColor clearColor];
scroll.pagingEnabled=YES;
scroll.contentSize = CGSizeMake(320*33, 300);
CGFloat x=0;
for(int i=1;i<34;i++)
{
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(x+0, 0, 320, 460)];
[image setImage:[UIImage imageNamed:[NSString stringWithFormat:#"page-%d.jpg",i]]];
[scroll addSubview:image];
[image release];
x+=320;
}
[self.view addSubview:scroll];
scroll.showsHorizontalScrollIndicator=NO;
[scroll release];

Changing the content of the UIScrollView iPhone

I am trying to create a scrollable menu bar (which I am succeeding in doing) but now instead I want to add a large button with an image centred in the button, instead of taking up the total width and height of the button
THis is my current code:
[scrollView setBackgroundColor:[UIColor whiteColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
UIImage *image;
UIImageView *iv;
UIButton *button;
count = [returned count];
for (int i=0; i<count; i++)
{
image = [UIImage imageNamed:#"coat2.jpg"];
iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,105,120)];
button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,105,120)];
[button setAlpha:100];
[button setTag:1];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
iv.image = image;
iv.tag=#"coat2.jpg";
iv.userInteractionEnabled = YES;
[iv insertSubview:button atIndex:0];
[button release];
[scrollView addSubview:iv];
[iv release];
}
[scrollView setScrollEnabled:YES];
[self layoutScrollImages];
scrollView.delegate = self;
the above code creates an image and button over it where the button and image are of the exact same width and height. I want the button to be significantly larger both in height and width.
Is this possible?
Thanks!
2nd attempt (using help from Joseph Tura)
[scrollView setBackgroundColor:[UIColor whiteColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = YES;
scrollView.scrollEnabled = YES;
UIImage *image;
count = [returned count];
for (int i=0; i<count; i++)
{
image = [UIImage imageNamed:#"coat2.jpg"];
iv = [[UIImageView alloc] initWithFrame:CGRectMake(0,20,105,120)];
button = [[ProductButton alloc] initWithFrame:CGRectMake(0,20,105,120)];
[button setAlpha:100];
[button setTag:1];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
iv.tag=tag;
iv.userInteractionEnabled = YES;
iv.image = myimage;
UIView *uiView = [[UIView alloc] initWithFrame:CGRectMake(0,20,105,120)];
[uiView addSubview:iv];
button = [[ProductButton alloc] initWithFrame:CGRectMake(0,0,210,240)];
button.center = CGPointMake(uiView.frame.size.width /2, uiView.frame.size.height/2);
[button setTag:1];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[uiView addSubview:button];
[scrollView addSubview:uiView];
}
[scrollView setScrollEnabled:YES];
[self layoutScrollImages];
scrollView.delegate = self;
-----
-(void)layoutScrollImages
{
//UIImageView *view = nil;
UIView *view = nil;
NSArray *subviews = [scrollView subviews];
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += 110;
}
}
[scrollView setContentSize: CGSizeMake(count*110, [scrollView bounds].size.height)];
}
Why not put both elements inside a UIView?
UIView *aView = [[UIView alloc] initWithFrame:iv.frame];
[aView addSubview:iv];
button = [[UIButton alloc] initWithFrame:CGRectMake(0,0,210,240)];
button.center = CGPointMake(aView.frame.size.width / 2, aView.frame.size.height / 2);
[aView addSubview:button];