This is really basic stuff, but I can't seem to get it right (I'm new to programming). What I'm trying to do is, have my Xcode 5 App detect if an image with a certain name (say for ex. #"go.png" is pressed or touched. How can I do this? There are only two buttons, so I've been doing it with the (touchLocation.y and .x ...) method, but I need to have the Button being pressed method now. I have pasted the code below. I really appreciate your help everyone.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];
switch (_flyGo) {
case HeadMenu:
if (touchLocation.y < self.size.height * 0.7) {
} else if (touchLocation.x < self.size.width * 0.3) {
[self switchNewFly:FlyTutorial];
} else {
[self giveRatingToApp];
}
break;
case FlyTutorial:
[self switchToFly];
break;
case FlyStatePlay:
[self flyPlayer];
break;
case FlyDisplayFalling:
break;
case FlyDisplayScore:
break;
case FlyDisplayDone:
if (touchLocation.x < self.size.width * 0.6) {
[self switchNewFly:FlyDisplayTutorial];
} else {
[self ShareMyScore];
}
break;
}
//I would like to get an image for ex. "flyer.png" to be detected as a touch instead of using (touchLocation.x or .y < self.size.width * 0.6)
Thanks!!!!
Yes, you can
Add touch recogniser to UIImageView (don't forget to set imgView.userInteractionEnabled = YES;
). Also set tag property for imgView to some value.
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapAll:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
[imgView addGestureRecognizer:tapRecognizer];
Then
- (void)handleTapAll:(UITapGestureRecognizer *)recognizer {
UIImageView *img = (UIImageView *)recognizer.view;
if(img.tag == some_vale) //your code
}
ViewController.h
#interface ViewController : UIViewController<UIGestureRecognizerDelegate>
......
ViewController.m
UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
imageView.image = [UIImage imageNamed:#"yourimage.png"];
imageView.userInteractionEnabled = YES;
imageView.exclusiveTouch = YES;
imageView.multipleTouchEnabled = YES;
// Add tap gesture
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[tap setNumberOfTapsRequired:1];
tap.delegate = self;
[imageView addGestureRecognizer:tap];
[self.view addSubview:imageView];
- (IBAction)handleTap:(UIGestureRecognizer *)recognizer { //Your code here }
Related
Hello fellow programmers.,
I want to create dynamic UIView of size 100X100 around my double click on Main UIView (Everytime I double click diffrant place). I totally don't have any idea about it. So can't provide any code for it. Anyone can help??
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [[touches allObjects] objectAtIndex: 0];
CGPoint currentPos = [myTouch locationInView: self.view];
NSLog(#"Point in myView: (%f,%f)", currentPos.x, currentPos.y);
UIView*hover = [[UIView alloc] initWithFrame:CGRectMake(currentPos.x - 50, currentPos.y - 50 , 100, 100)];
hover.backgroundColor = [UIColor grayColor];
[self.view addSubview:hover];
}
try this code
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(addDynamicView:)];
self.view.userInteractionEnabled = TRUE;
tapGesture.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:tapGesture];
adding the custom view
- (void)addDynamicView:(UITapGestureRecognizer*)aGesture{
/** add view with specified frame **/
CGRect rect = CGRectMake(10, 10, 100, 100);
UIView *lView = [[UIView alloc] initWithFrame:rect];
lView.backgroundColor = [UIColor blackColor];
[self.view addSubview:lView];
}
In my iOS application, I use UITouchGestureRecognizer to drag/drop, zoom and rotate UIImageView. I also store the information on the last position of the ImageView to retrieve it when the app restarts.
That was working well with iOS7 but I have recently started to test with iOS8 and I'm facing some issues, especially with the zoom. Whenever the imageview has a non-0 angle in the rotation parameter, the zoom is not acting like expected and reduces the size of the image.
I don't understand why the OS change does that, here is my code:
-(void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:YES];
[self loadAllImages];
for (GIFavorite *favorite in self.favorites){
UIImage *image = favorite.image;
ImageView *imageView = [[ImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
imageView.center = CGPointMake(favorite.x, favorite.y);
imageView.transform = CGAffineTransformMakeRotation(favorite.rotation);
imageView.transform = CGAffineTransformScale(imageView.transform, favorite.scale, favorite.scale);
imageView.image = image;
imageView.userInteractionEnabled = YES;
UIPanGestureRecognizer * panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
panRecognizer.delegate = self;
[imageView addGestureRecognizer:panRecognizer];
UIRotationGestureRecognizer * rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(handleRotate:)];
rotationRecognizer.delegate = self;
[imageView addGestureRecognizer:rotationRecognizer];
UIPinchGestureRecognizer * pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinch:)];
pinchRecognizer.delegate = self;
[imageView addGestureRecognizer:pinchRecognizer];
[self.view addSubview:imageView];
}
}
I guess the way I am doing the transforms is wrong because when I put the CGAffineTransformMakeRotation instruction after the CGAffineTransformScale instead of reducing the size it increases it.
Where am I doing things wrong?
Edit: the code for handleRotate
- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
ImageView *imageView = recognizer.view;
CGFloat radians = atan2f(imageView.transform.b, imageView.transform.a);
favorite.rotation = radians;
recognizer.rotation = 0;
}
First, you need to make your view controller implements UIGestureRecognizerDelegate.
SWIFT 3 :-
func setUpGestures () {
let panGesture = UIPanGestureRecognizer(target: self, action:#selector(self.handlePanGesture(gesture:)))
self.imageView.addGestureRecognizer(panGesture)
let rotationGesture = UIRotationGestureRecognizer(target: self, action:#selector(self.handleRotationGesture(gesture:)))
self.imageView.addGestureRecognizer(rotationGesture)
let pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(self.handlePinchGesture(gesture:)))
self.imageView.addGestureRecognizer(pinchGesture)
}
func handlePinchGesture(gesture: UIPinchGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{
//print("UIPinchGestureRecognizer")
gesture.view?.transform = (gesture.view?.transform)!.scaledBy(x: gesture.scale, y: gesture.scale)
gesture.scale = 1.0
}
}
func handleRotationGesture(gesture: UIRotationGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{
//print("UIRotationGestureRecognizer")
gesture.view?.transform = (gesture.view?.transform)!.rotated(by: gesture.rotation)
gesture.rotation = 0
}
}
func handlePanGesture(gesture: UIPanGestureRecognizer) {
if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{
//print("UIPanGestureRecognizer")
let translation = gesture.translation(in: view)
gesture.view?.transform = (gesture.view?.transform)!.translatedBy(x: translation.x, y: translation.y)
gesture.setTranslation(CGPoint(x: 0, y: 0), in: view)
}
}
Hope, this is what you're looking for. Any concern get back to me. :)
First, you need to make your view controller implements UIGestureRecognizerDelegate.
Then add the gesture recognizers to your UIView (I see that you already did that, but just putting it here again to have the solution in one place).
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePan:)];
panGesture.delegate = self;
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(handleRotate:)];
rotationGesture.delegate = self;
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:#selector(handlePinch:)];
pinchGesture.delegate = self;
[yourView addGestureRecognizer:panGesture];
[yourView addGestureRecognizer:rotationGesture];
[yourView addGestureRecognizer:pinchGesture];
Then, this is how your handle... methods should be:
- (void)handlePinch:(UIPinchGestureRecognizer *)pinchRecognizer
{
UIGestureRecognizerState state = [pinchRecognizer state];
if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGFloat scale = [pinchRecognizer scale];
[pinchRecognizer.view setTransform:CGAffineTransformScale(pinchRecognizer.view.transform, scale, scale)];
[pinchRecognizer setScale:1.0];
}
}
- (void)handleRotate:(UIRotationGestureRecognizer *)rotationGestureRecognizer {
UIGestureRecognizerState state = [rotationGestureRecognizer state];
if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGFloat rotation = [rotationGestureRecognizer rotation];
[rotationGestureRecognizer.view setTransform:CGAffineTransformRotate(rotationGestureRecognizer.view.transform, rotation)];
[rotationGestureRecognizer setRotation:0];
}
}
- (void)handlePan:(UIPanGestureRecognizer *)panRecognizer {
UIGestureRecognizerState state = [panRecognizer state];
if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [panRecognizer translationInView:panRecognizer.view];
[panRecognizer.view setTransform:CGAffineTransformTranslate(panRecognizer.view.transform, translation.x, translation.y)];
[panRecognizer setTranslation:CGPointZero inView:panRecognizer.view];
}
}
And this is how it looks:
try doing these..
first in your ViewControllers .h do these..
#interface RotationViewController : UIViewController<UIGestureRecognizerDelegate>//Add these..
{
UIView *testView;
}
now do these in .m file of your ViewController..
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
testView=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 160, 160)];
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 160, 160)];
[imageView setImage:[UIImage imageNamed:#"ImageName.png"]];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[testView addSubview:imageView];
[self.view addSubview:testView];
UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:#selector(handleRotate:)];
[testView addGestureRecognizer:rotationGestureRecognizer];
}
-(void) handleRotate:(UIRotationGestureRecognizer *)rotationGestureRecognizer
{
testView.transform = CGAffineTransformRotate(testView.transform, rotationGestureRecognizer.rotation);
rotationGestureRecognizer.rotation = 0.0;
}
i hope it helps..
I have a UIScrollView .In that i have one ImageView.
I want to get UITOUCH point .
i know i can get location by
- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
CGPoint touchPoint = [tapRecognizer locationInView: _tileMap]
}
But i want uitouch on image.
PLease help me.
try like this,
- (void)handleTap:(UITapGestureRecognizer *)tapRecognizer
{
CGPoint touchPoint = [tapRecognizer locationInView: tapRecognizer.view]
}
AND set image view userInteractionEnabled to YES
UIImageView *image = [[UIImageView alloc]init];
image.frame = CGRectMake(55, 30, 50, 40);
image.image = [UIImage imageNamed:#"Default.png"];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
singleTap.numberOfTapsRequired = 1;
self.view.tag =10;
image.userInteractionEnabled = YES;
[image addGestureRecognizer:singleTap];
Having added a UITapGestureRecognized to a UIView, how do I parse the view for ie. changing the background color during the tap? The purpose of this is to imitate a button click.
UIView *locationView = [[UIView alloc] init];
locationView.tag = 11;
locationView.backgroundColor = [UIColor clearColor];
locationView.userInteractionEnabled = YES;
[locationView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(promptForLocation)]];
Let's say I want locationView.backgroundColor = [UIColor blueColor] right after the tap gesture. Would you just implement it in the target action or is there a specific implementation for this?
Update:
This is my final code inspired by #0x7fffffff
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ...
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressDetectedLocation:)];
longPress.allowableMovement = 50.0f;
longPress.minimumPressDuration = 0.05;
UILongPressGestureRecognizer *longPress2 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressDetectedPhoto:)];
longPress2.allowableMovement = 50.0f;
longPress2.minimumPressDuration = 0.05;
[leftView addGestureRecognizer:longPress];
[rightView addGestureRecognizer:longPress2];
// ...
}
- (BOOL)longPressDetected:(UILongPressGestureRecognizer *)sender {
if ([self.view hasFirstResponder]) {
return NO;
}
if (sender.state == UIGestureRecognizerStateBegan) {
[sender.view setBackgroundColor:[UIColor colorWithRed:(4/255.0) green:(129/255.0) blue:(241/255.0) alpha:1]];
} else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateFailed) {
[sender.view setBackgroundColor:[UIColor clearColor]];
}
CGPoint location = [sender locationInView:sender.view];
return sender.state == UIGestureRecognizerStateEnded && location.x > 0 && location.x < sender.view.frame.size.width && location.y > 0 && location.y < sender.view.frame.size.height;
}
- (void)longPressDetectedLocation:(UILongPressGestureRecognizer *)sender {
if ([self longPressDetected:sender]) {
[self promptForLocation];
}
}
- (void)longPressDetectedPhoto:(UILongPressGestureRecognizer *)sender {
if ([self longPressDetected:sender]) {
[self promptForPhoto];
}
}
Considering you're trying to imitate a button click, I'm assuming you'd want the view to revert to its original state after the touch ends. To do this, you'll want to use a UILongPressGestureRecognizer instead of a UITapGestureRecognizer.
With a tap gesture, the recognizer isn't detected until the touch ends, so you'd effectively be highlighting the view as soon as you lift your finger. To get around this, use the long press gesture its minimumPressDuration property set to 0.0. Then in its selector, check the state of the sending gesture; If it has just begun, change the background color, and if it has ended revert back to the original color.
Here's an example:
- (void)viewDidLoad {
[super viewDidLoad];
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 100.0f, 100.0f)];
[myView setBackgroundColor:[UIColor redColor]];
[self.view addSubview:myView];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(longPressDetected:)];
[longPress setAllowableMovement:50.0f];
[longPress setMinimumPressDuration:0.0];
[myView addGestureRecognizer:longPress];
}
- (void)longPressDetected:(UILongPressGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
[sender.view setBackgroundColor:[UIColor blueColor]];
}else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateFailed) {
[sender.view setBackgroundColor:[UIColor redColor]];
}
NSLog(#"%d",sender.state);
}
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tappedMethod:)];
[locationView addGestureRecognizer:gr];
This is method
-(void)tappedMethod:(UIGestureRecognizer *)ge
{
// write relavent code here;
locationView.backgroundColor = [UIColor blueColor];
}
I need to handle touch on my object when it move across my screen. When touchesBegan called I need to hide my object.
this is my code in UIViewController:
- (void)win {
for (NSInteger i = 1; i <= 6; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"rocket_%d.png", i]]];
imageView.tag = 1000;
CGSize size = imageView.image.size;
[imageView setFrame:CGRectMake(200 + (i * 60), 500, size.width, size.height)];
[self.view addSubview:imageView];
[rockets addObject:imageView];
[imageView setUserInteractionEnabled:YES];
}
[self startRockets];
}
- (void)startRockets {
CGFloat timeInterval = 1.0;
for (UIImageView *imageView in rockets) {
[UIView animateWithDuration:5.0 delay:timeInterval options:UIViewAnimationOptionAllowUserInteraction animations:^{
[imageView setFrame:CGRectMake(imageView.frame.origin.x, 0, imageView.frame.size.width, imageView.frame.size.height)];
} completion:nil];
timeInterval += 1.0;
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
NSArray *views = [self.view subviews];
for (UIView *v in views) {
if (v.tag == 1000) {
if (CGRectContainsPoint(v.frame, touchLocation) == YES) {
[v setHidden:YES];
}
}
}
}
Maybe you are missing the AllowAnimatedContent. Try changing your
UIViewAnimationOptionAllowUserInteraction
to
UIViewAnimationOptionAllowUserInteraction | UIViewAnimationOptionAllowAnimatedContent
...another way might be to use UITapGestureRecognizer :
- (void)createViews {
rockets = [[NSMutableArray alloc] init];
for (NSInteger i = 1; i <= 6; i++) {
UIView *view = [UIView new];
view.backgroundColor = [UIColor redColor];
view.frame = CGRectMake((i * 60), 100, 50, 50);
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(rocketTouched:)];
[view addGestureRecognizer:tap];
[self.view addSubview:view];
[rockets addObject:view];
}
[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:#selector(moveViews) userInfo:nil repeats:YES];
}
- (void)moveViews {
for (UIView * view in rockets) {
[UIView animateWithDuration:5.0 delay:0.0 options:UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionAllowAnimatedContent animations:^{
view.frame = CGRectMake(view.frame.origin.x -10, 0, view.frame.size.width, view.frame.size.height);
} completion:nil];
}
}
- (void)rocketTouched:(UITapGestureRecognizer*)tap {
tap.view.hidden = YES;
}
You need also check this if you need to detect intersection with object.
I have edited touchesBegan method and it works.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
for (UIImageView *imageView in rockets) {
CALayer *bbl1ObjPresentationLayer = (CALayer*)[imageView.layer presentationLayer];
if(CGRectIntersectsRect(bbl1ObjPresentationLayer.frame, CGRectMake(touchLocation.x, touchLocation.y, 1, 1))) {
NSLog(#"intersect");
//They are intersecting
}
}
}
if you do it using CALayer insted of UIImageView object this work correct. Check this answer link