While I know how to use gesture recognizer in a view-based application,but when I apply the same ideas in a OpenGLSE-based application:
for example,
I add a TapGestureRecognizer,and when I tap on the EAGLView,it crashes.
So can anyone show me a standard usage of UITapGestureRecognizer in an OpenGLES-based application?
best wishes.
Here some sample code from one of my opengles games with gesture support. (Doesn't crash and hope it helps)
- (void)viewDidLoad {
[super viewDidLoad];
CGRect rect = [[UIScreen mainScreen] bounds];
rect.size.height = 320;
rect.size.width = 480;
rect.origin.x = 0;
rect.origin.y = 0;
glView = [[EAGLView alloc] initWithFrame:rect pixelFormat:GL_RGB565_OES depthFormat:GL_DEPTH_COMPONENT16_OES preserveBackbuffer:NO];
[self.view addSubview: glView];
[glView addSubview: minimapView];
if(!shell->InitApplication())
printf("InitApplication error\n");
[NSTimer scheduledTimerWithTimeInterval:(1.0 / kFPS) target:self selector:#selector(update) userInfo:nil repeats:YES];
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(Panned:)];
[glView addGestureRecognizer:[pan autorelease]];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(Tapped:)];
[glView addGestureRecognizer:[tap autorelease]];
UITapGestureRecognizer *dbltap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(DoubleTapped:)];
[dbltap setNumberOfTapsRequired:2];
[glView addGestureRecognizer:[dbltap autorelease]];
UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(LongPressed:)];
[glView addGestureRecognizer:[longpress autorelease]];
}
And the selector function
- (void) LongPressed:(UILongPressGestureRecognizer*)sender{
NSLog(#"Long Pressed");
}
Related
touchBegin is called only once after viewWillAppear method executed.
I have added UITouch and UISwipe recognizer, enabled user interaction and enabled all gestures but GMSMapView respond only once.
self.mapView_ = [GMSMapView mapWithFrame:CGRectMake(0, 0, 320, 206) camera:nil];
[self.mapView_ setUserInteractionEnabled:YES];
[self.mapView_.settings setAllGesturesEnabled:YES];
[self.view addSubview: mapView_];
[self.view sendSubviewToBack:self.mapView_]; // I have to do this because
self.mapView_.myLocationEnabled = YES;
mapView_.delegate = self;
UIGestureRecognizer *uiGestureRecognizer = [[UIGestureRecognizer alloc]init];
uiGestureRecognizer.delegate = self;
UISwipeGestureRecognizer * gestureRecognizer = [[UISwipeGestureRecognizer alloc]init];
gestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp & UISwipeGestureRecognizerDirectionRight & UISwipeGestureRecognizerDirectionLeft & UISwipeGestureRecognizerDirectionDown;
[gestureRecognizer addTarget:self action:#selector(selecterMethod)];
self.mapView_.gestureRecognizers = [NSArray arrayWithObjects:uiGestureRecognizer,gestureRecognizer, nil];
I have a blank screen on which i click camera button and take photo and than that photo appears on my blank screen as a UIVIew and i'm adding multiple images from Camera to blank screen. The problem is sometimes i can add multiple images on blank screen but sometimes when i capture image and than all images on blank screen disappear and just shows the current image.
And in LOG. it show me Received memory warning.
this is my code. i think i m now releasing my objects properly. but when i am adding [object release] in the end my app crashes.
-(void)AddImagesToCanvasWithGesture{
if(imageFromPicker.size.width > imageFromPicker.size.height || imageFromPicker.size.width == imageFromPicker.size.height)
{
holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 180, 160)];
}
else{
holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 240)];
}
UIImageView *imageview = [[[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:imageFromPicker];
//[imageview setTag:101];
//[holderView setTag:102];
//NSLog(#"Tag By Default %d",(arc4random()%100)+10);
[holderView setTag:(int)objectDelegate.tagForHolderView];
[imageview setTag:((int)objectDelegate.tagForHolderView)+1];
[holderView addSubview:imageview];
//[imageview canBecomeFirstResponder];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(gestureHandler:)];
[holderView addGestureRecognizer:gestureRecognizer];
[holderView.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[holderView.layer setBorderWidth: 3.0];
[self.view addSubview:holderView];
objectDelegate.tagForHolderView+=2;
for(UIButton *b in self.view.subviews) {
if([b isKindOfClass:[UIButton class]]) {
[self.view bringSubviewToFront:b];
}
[self.view sendSubviewToBack:imageViewForBackground];
//[holderView setHidden:YES];
}
}
how can i release them and where should i call the release method.
From the code snippet you have added above, it's obvious the you are not releasing objects properly. You own every object that you init or retain or copy and you are responsible to release them unless you are using ARC. (The code above is fine for ARC).
You have ownership of all the gesture recognizers and image view and you should release them as soon as you no longer need them. For instance... imageView object must be released after
[holderView addSubview:imageview];
and
pinchRecognizer should be released right after
[holderView addGestureRecognizer:pinchRecognizer];
Same goes for other gesture recognizers.
I guess you have similar kind of problems in other parts of your code too and eventually the app receives memory warning.
When you have a memory problem, if you have a method named
- (void)didReceiveMemoryWarning
it will get called and allow you to free up space. If you are using ARC, just set your UIImageView to nil, if not, call:
[imageview release];
I have a blank screen on which i click camera button and take photo and than that photo appears on my blank screen as a UIVIew and i'm adding multiple images from Camera to blank screen. The problem is sometimes i can add multiple images on blank screen but sometimes when i capture image and than all images on blank screen disappear and just shows the current image.
- (IBAction)openCameraOnAddButton:(id)sender {
//NSLog(#"openCameraOnAddButton");
[AddImagesToCanvasView setHidden:YES];
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
UIImage *image = [[info objectForKey:#"UIImagePickerControllerOriginalImage"] retain];
UIView *holderView;
if(image.size.width > image.size.height || image.size.width == image.size.height)
{
holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 180, 160)];
}
else{
holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 240)];
}
UIImageView *imageview = [[UIImageView alloc] initWithFrame:[holderView frame]];
[imageview setImage:image];
NSLog(#"Tag By Default %d",(arc4random()%100)+10);
[holderView setTag:(int)objectDelegate.tagForHolderView];
[imageview setTag:((int)objectDelegate.tagForHolderView)+1];
[holderView addSubview:imageview];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(scale:)];
[pinchRecognizer setDelegate:self];
[holderView addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(rotate:)];
[rotationRecognizer setDelegate:self];
[holderView addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[holderView addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[holderView addGestureRecognizer:tapRecognizer];
UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(gestureHandler:)];
[holderView addGestureRecognizer:gestureRecognizer];
[holderView.layer setBorderColor: [[UIColor whiteColor] CGColor]];
[holderView.layer setBorderWidth: 3.0];
[self.view addSubview:holderView];
objectDelegate.tagForHolderView+=2;
}
Not sure but as you are doing all the operation UIImagePickerCotroller's delegate methods sometimes it may not work.
I faced same problem while storing the image in document directory & to resolve this i had created new Thread in didFinishPickingMediaWithInfo method & moved entire code in that method. Then after it works fine for me. Try to do that.
Im Having MapView.. I added this as a subview of ViewController's View. I have the following Code in ViewDidLoad:
[self.view addSubview:mapView];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(mapLongPress:)];
longPressGesture.minimumPressDuration = 2;
[mapView addGestureRecognizer:longPressGesture];
[longPressGesture release];
And ,
- (void)mapLongPress:(UILongPressGestureRecognizer *)gestureRecognizer{
NSLog(#"Gesture");
if(gestureRecognizer.state == UIGestureRecognizerStateBegan){
CGPoint touchLocation = [gestureRecognizer locationInView:mapView];
CLLocationCoordinate2D coordinate;
coordinate = [mapView convertPoint:touchLocation toCoordinateFromView:mapView];
These i Got from StackOverFlow.. But Its not Working.. Did i need to do anything more in that?
Try adding [self.view addSubview:mapView]; after [mapView addGestureRecognizer:longPressGesture];
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(mapLongPress:)];
longPressGesture.minimumPressDuration = 2;
longPressGesture.delegate = self;
[self.mapView addGestureRecognizer:longPressGesture];
[self.view addSubview:mapView];
[longPressGesture release];
Just add the UIGestureRecognizerDelegate to ViewController and do the above code given by me!!!
Try this:
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
initWithTarget:self action:#selector(mapLongPress:)];
lpgr.minimumPressDuration = 2.0;
[self.mapView addGestureRecognizer:lpgr];
This is using ARC, so I am not sure if you should release the gesture at this moment right now. Try it without releasing it, then try it with releasing it. See if that affects the gesture.
I am trying to attach a UITapGestureRecognizer to a webview that i am creating and then remove that view when a user taps the webview. Below is my sample code. What am i doing wrong? Thanks!
- (void) setupPuzzle1
{
puzzleDuration--;
//Create object circle
UIImageView *circleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"red-circle.png"] highlightedImage:[UIImage imageNamed:#"red-circle.png"]];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(removeImage:)];
recognizer.numberOfTapsRequired = 1;
recognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;
[circleView addGestureRecognizer:recognizer];
[recognizer release];
int x = rand()%280;
int y = rand()%420;
circleView.frame = CGRectMake(x,y,40,40);
[self.view addSubview:circleView];
if (puzzleDuration > 0)
{
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(setupPuzzle1) userInfo:nil repeats:NO];
}
}
#pragma mark - UITapGestureRecognizer methods
- (void)removeImage:(UITapGestureRecognizer *)recognizer
{
NSLog(#"Remove Image");
[[recognizer view] removeFromSuperview];
}
#end
I think you weren't talking about UIWebView at all. If you did, your code didn't reflect that. As such you are dealing with UIImageView and the reason is pretty straightforward as any UIImageView object by default has its userInteractionEnabled set to NO. You should change it to YES. So add this line,
circleView.userInteractionEnabled = YES;
and you aren't releasing it either. So add
[circleView release];
after you add it as a subview.