based on how to add on touch and detect UIImageView on view so i could move it around and not adding one on top of it?
i got around of checking CGRectIntersectsRect single uiimageview. how will i need to check multiple uiimageview?
CORRECT
WRONG
-(void)initImagesAndGesture
{
UIImage *img = [UIImage imageNamed:#"beer.png"];
imgView1 = [[UIImageView alloc]initWithImage:img];
[imgView1 setFrame:CGRectMake(0, 0, 75, 115)];
[imgView1 setUserInteractionEnabled:YES];
UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[imgView1 addGestureRecognizer:recognizer1];
[self.view addSubview:imgView1];
img = [UIImage imageNamed:#"cups.png"];
imgView2 = [[UIImageView alloc]initWithImage:img];
[imgView2 setFrame:CGRectMake(200, 240, 64, 75)];
[imgView2 setUserInteractionEnabled:YES];
recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[imgView2 addGestureRecognizer:recognizer1];
[self.view addSubview:imgView2];
}
-(void)handlePan1:(UIPanGestureRecognizer *)recognizer
{
if (!(CGRectIntersectsRect(imgView1.frame, imgView2.frame)))
{
CGPoint pre_moveLocation = [recognizer locationInView:recognizer.view];
NSLog(#"previous location %#",NSStringFromCGPoint(pre_moveLocation));
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:recognizer.view];
}
else
{
NSLog(#"intersect!");
CGPoint pre_moveLocation = [recognizer locationInView:recognizer.view];
NSLog(#"previous location %#",NSStringFromCGPoint(pre_moveLocation));
recognizer.view.center = CGPointMake(pre_moveLocation.x, pre_moveLocation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:recognizer.view];
//[imgView2 setFrame:CGRectMake(200, 240, 64, 75)];
//[imgView1 setFrame:CGRectMake(0, 0, 75, 115)];
}
}
-(void)addImgView:(UIPanGestureRecognizer *)recognizer
{
NSLog(#"tappppp");
UIImage *img = [UIImage imageNamed:#"beer.png"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
CGPoint tapLocation = [recognizer locationInView:recognizer.view];
[imgView setCenter:CGPointMake(tapLocation.x,tapLocation.y)];
[imgView setUserInteractionEnabled:YES];
UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[imgView addGestureRecognizer:recognizer1];
[self.view addSubview:imgView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self initImagesAndGesture];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(addImgView:)];
tapRecognizer.numberOfTapsRequired = 2;
tapRecognizer.numberOfTouchesRequired = 1;
[tapRecognizer setDelegate:self];
self.view.userInteractionEnabled = YES;
[self.view addGestureRecognizer:tapRecognizer];
}
Just iterate through all subviews...
// targetView is the one you are checking...
- (BOOL)isViewIntersectingAnyOtherViews:(UIView*)targetView {
for (UIView *view in self.view.subviews) {
if (view == targetView) continue; // Don't check against your own view
if (CGRectIntersectsRect(view, thisView)) {
return YES;
}
}
return NO;
}
Related
Adding UIImageView on UIScrollView and adding UIPanGestureRecognizer to it
UIImageView* lbl = [[UIImageView alloc]init];
NSString* tempStr = [NSString stringWithFormat:#"images/%#",[self.cloudArray objectAtIndex:i]];
NSString* imgPath = [self appendDocumentDirectoryPath:tempStr];
lbl.image = [[[UIImage alloc] initWithContentsOfFile:imgPath] autorelease];
[lbl setUserInteractionEnabled:YES];
//add gestures
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(moveImage:)];
[panGesture setMinimumNumberOfTouches:1];
[panGesture setMaximumNumberOfTouches:1];
[lbl addGestureRecognizer:panGesture];
[panGesture release];
CGSize expectedLabelSize = CGSizeMake(150, height);
lbl.frame = CGRectMake(pntX, pntY, expectedLabelSize.width, height);
[rectFrame addObject:[NSValue valueWithCGRect:lbl.frame]];
lbl.tag = i;
[cloudScrollView addSubview:lbl];
[lbl release];
lbl = nil;
pntX = pntX + space + expectedLabelSize.width;
[cloudScrollView setContentSize:CGSizeMake([cloudArray count]*170, 160)];
#pragma mark METHOD TO CATCH MOVE GESTURE EVENTS
- (void)moveImage:(UIPanGestureRecognizer *)gesture
{
if (gesture.state == UIGestureRecognizerStateChanged) {
location1 = [gesture locationInView:cloudScrollView];
view1 = [gesture view];
if (isFirstTime) {
isFirstTime = NO;
draggedTag = view1.tag;
}
[view1 setCenter:CGPointMake(location1.x, location1.y)];
}
if (gesture.state == UIGestureRecognizerStateEnded) {
CGPoint pnt = [gesture locationInView:customTableView];
NSIndexPath* indexPath = [customTableView indexPathForRowAtPoint:pnt];
NSLog(#"indexpath = %d",indexPath.row);
if (index >= 0) {
[self.ansArray replaceObjectAtIndex:indexPath.row withObject:[self.cloudArray objectAtIndex:draggedTag]];//[self.cloudArray objectAtIndex:draggedTag]
[customTableView reloadData];
}
CGRect draggedItemRect = [[rectFrame objectAtIndex:draggedTag] CGRectValue];
view1.frame = draggedItemRect;
isFirstTime = YES;
}
}
Currently it is saving all images but i want to save only the one which is long pressed not all images in one shot only the one user selects
- (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 addGestureRecognizer:tap];
[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];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
_imageView.tag = 128;
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
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];
//[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
- (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 {
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
UIImageWriteToSavedPhotosAlbum(imageView.image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
}
}
How to code for that
Thanks for help
In the handleLongPress: function, use this:
UIGestureRecognizer *gestureRecognizer;
CGPoint gesturePoint = [gestureRecognizer locationInView:self.view];
Then check all of your buttons using a for loop like you have there. Use this to check if it is the pressed image:
if(CGRectContainsPoint(myButton.frame,gesturePoint)){
//save the image stored in myButton
//break will exit the for loop early, so if the view comes early in the list,
//it doesn't have to check the rest.
break;
}
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.
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;
I encountered a problem with my UILongPressGestureRecognizer and UIPanGestureRecognizer.
i wish to long press in my self.view to add a view with UIPanGestureRecognizer in my self.view.
However the UIPanGestureRecognizer is not recognizing.
did i missed anything ?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//[self initImagesAndGesture];
UILongPressGestureRecognizer *tapRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(addImgView:)];
tapRecognizer.numberOfTouchesRequired = 1;
tapRecognizer.minimumPressDuration = 0.7;
[tapRecognizer setDelegate:self];
self.view.userInteractionEnabled = YES;
[self.view addGestureRecognizer:tapRecognizer];
}
-(void)addImgView:(UILongPressGestureRecognizer *)recognizer
{
NSLog(#"tappppp");
if(UIGestureRecognizerStateBegan == recognizer.state) {
// Called on start of gesture, do work here
NSLog(#"UIGestureRecognizerStateBegan");
UIImage *img = [UIImage imageNamed:#"beer.png"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
UILabel *timeStamp = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
timeStamp.text = [NSString stringWithFormat:#"%f",[NSDate date]];
UIView *drinkView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
[drinkView setUserInteractionEnabled:YES];
CGPoint tapLocation = [recognizer locationInView:recognizer.view];
[timeStamp setCenter:CGPointMake(tapLocation.x, tapLocation.y)];
[imgView setCenter:CGPointMake(tapLocation.x,tapLocation.y)];
[drinkView addSubview:imgView];
[drinkView addSubview:timeStamp];
[drinkView setUserInteractionEnabled:YES];
[imgView setUserInteractionEnabled:YES];
[timeStamp setUserInteractionEnabled:YES];
UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[recognizer1 setDelegate:self];
[drinkView addGestureRecognizer:recognizer1];
[self.view addSubview:drinkView];
}
}
-(void)addImgView:(UILongPressGestureRecognizer *)recognizer
{
NSLog(#"tappppp");
if(UIGestureRecognizerStateBegan == recognizer.state) {
// Called on start of gesture, do work here
NSLog(#"UIGestureRecognizerStateBegan");
UIImage *img = [UIImage imageNamed:#"beer.png"];
UIImageView *imgView = [[UIImageView alloc]initWithImage:img];
UILabel *timeStamp = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 30)];
timeStamp.text = [NSString stringWithFormat:#"%f",[NSDate date]];
UIView *drinkView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 75,115)];
drinkView.backgroundColor=[UIColor redColor];
[drinkView setUserInteractionEnabled:YES];
CGPoint tapLocation = [recognizer locationInView:recognizer.view];
[drinkView setCenter:CGPointMake(tapLocation.x,tapLocation.y)];
[drinkView setUserInteractionEnabled:YES];
[imgView setUserInteractionEnabled:YES];
[timeStamp setUserInteractionEnabled:YES];
[drinkView addSubview:imgView];
[drinkView addSubview:timeStamp];
UIPanGestureRecognizer *recognizer1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:#selector(handlePan1:)];
[recognizer1 setDelegate:self];
[drinkView addGestureRecognizer:recognizer1];
[self.view addSubview:drinkView];
}
if(UIGestureRecognizerStateChanged == recognizer.state) {
// Do repeated work here (repeats continuously) while finger is down
NSLog(#"UIGestureRecognizerStateChanged");
}
if(UIGestureRecognizerStateEnded == recognizer.state) {
// Do end work here when finger is lifted
NSLog(#"UIGestureRecognizerStateEnded");
}
}