My UItouches is not detecting in my Slideshow nib file. What is the problem? Can anyone help?
#class Slideshow;
#interface RootViewController : UIViewController{
PreferencesController *preferencesController;
Slideshow *slideshow;}
Slideshow Implementation
#implementation Slideshow
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
NSLog(#"touches begin");
}
- (void)viewDidLoad
{
UIImageView *frontView = [[UIImageView alloc] initWithFrame:self.view.bounds];
frontView.backgroundColor = [UIColor blackColor];
frontView.image = [UIImage imageNamed:#"apple.png"];
frontView.userInteractionEnabled = YES;
[self.view addSubview:frontView];
[frontView release];
}#end
Slideshow is a UIViewController and touchesBegan is only called on a UIView. You need to subclass UIView to capture touches.
Related
i have a scrollview and inside a uiview that contains multiple subviews ( uiimageview). the problem is i can't pass touch gestures to the imageviews ..
#interface photoLibrary (){
UIView *view_cuImagini;
}
#property (nonatomic,retain) UIScrollView *scrl_images;
#end
#implementation photoLibrary
#synthesize scrl_images;
//..
- (void)viewDidLoad{
[super viewDidLoad];
//...
scrl_images.minimumZoomScale=0.4;
view_cuImagini=[[UIView alloc]initWithFrame:CGRectMake(scrl_images.frame.origin.x, scrl_images.frame.origin.y, scrl_images.frame.size.width, scrl_images.frame.size.height)];
view_cuImagini.backgroundColor=[UIColor blackColor];
[scrl_images addSubview:view_cuImagini];
}
-(void)AddImageViewWithFrame:(CGRect)frame andImage:(UIImage*)img andtag:(int)tag{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(imageTapped:)];
tempimg=[[UIImageView alloc] initWithFrame:frame];
UIImageView *bifat = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"bifat.jpg"]];
tempimg.image=img;
tempimg.tag=tag;
tap.delegate=self;
tempimg.userInteractionEnabled = YES;
view_cuImagini.userInteractionEnabled=YES;
[tempimg addGestureRecognizer:tap];
// [self.view addGestureRecognizer:tap];
[tempimg addSubview:bifat];
[view_cuImagini addSubview:tempimg];
}
-(void)imageTapped:(UITapGestureRecognizer *)rec{
if (rec.state==UIGestureRecognizerStateRecognized) {
NSLog(#"imageTouch with tag %i",rec.view.tag);
}
i want to be able to find the which tempimg was touched
does anybody has a solutions for this?
I am programmatically creating a view and a imageview while in viewControllerA for an another viewControllerB before I then goto viewControllerB. I have to do this, as I am using an image from the imagePickerController. However by doing this, touchesBegan does not function in viewControllerB
I have set UserInteractionEnabled to true/yes in both the attributes inspector of the xib and programmatically everywhere!
My xib is just a blank canvas, with one View. I have tried different settings with the connections inspector, but still no luck.
Here is my code.
viewControllerA.m
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
view = [[UIView alloc] initWithFrame:screenRect];
image = [info valueForKey:UIImagePickerControllerEditedImage];
SSViewController *psView = [[SSViewController alloc] initWithNibName:#"SSViewController" bundle:nil];
psView.view = [[UIView alloc] initWithFrame:screenRect];
[psView.view setUserInteractionEnabled:YES];
[picker pushViewController:psView animated:YES];
imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(20, 20, 280, 280);
[imageView setUserInteractionEnabled:YES];
[psView.imageView setUserInteractionEnabled:YES];
[psView.view addSubview:imageView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:psView action:#selector(endPS:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"done" forState:UIControlStateNormal];
button.frame = CGRectMake(140.0, 330.0, 80.0, 25.0);
[psView.view addSubview:button];
}
viewControllerB.h (SSViewController.h)
#interface SSViewController : UIViewController
{
IBOutlet UIImageView *imageView;
IBOutlet UIView *view;
}
#property (nonatomic,strong) IBOutlet UIImageView *imageView;
#property (nonatomic,strong) IBOutlet UIImage *image;
#property (nonatomic, strong) IBOutlet UIView *view;
- (IBAction)endPS:(id)sender;
#end
viewControllerB.m (SSViewController.m)
#implementation SSViewController
#synthesize imageView;
#synthesize image;
#synthesize view;
:
:
- (void)viewDidLoad
{
[view setUserInteractionEnabled:YES];
[imageView setUserInteractionEnabled:YES];
:
:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"touchesBegan");
:
You're doing a number of things wrong:
Don't push a view controller on to the UIImagePickerController. If you've presented it modally, which I hope you have, you need to dismiss it (in viewControllerA) and push your new view controller (viewControllerB) onto your own navigation controller. The fact that you're pushing onto this highly customised navigation controller is most likely why touchesBegan is not being called.
Why are you manually creating the view for viewControllerB? If you're loading it from a XIB file, it will have a perfectly good view ready for you. And don't define the view property in viewControllerB.h, it's already defined in UIViewController.h.
Don't forget to call [super viewDidLoad] in viewControllerB.m. This is a classic mistake that will cause you many headaches.
EDIT:
What I would personally have done is added a method on viewControllerB as such:
viewControllerB.h
#interface SSViewController : UIViewController
{
- (void)setImage:(UIImage *)image;
}
#end
viewControllerB.m
#implementation SSViewController
{
- (void)setImage:(UIImage *)image
{
// In case the image view already exists, remove it first.
[_imageView removeFromSuperview];
_imageView = [[UIImageView alloc] initWithImage:image];
_imageView.frame = CGRectMake(20, 20, 280, 280);
[self.view addSubview:_imageView];
}
}
#end
This way, in viewControllerA.m you can just create viewControllerB.m and call this setImage: method to let viewControllerB do all the work.
I use UITapGestureRecognizer to handle An ImageviewTap actions. In the main ViewController it works great. But when i Use another ViewController in my APP, and copy the same UITapRecognizer code I get an EXC_BAD_ACCESS code=1 address: 0x80759d3a error message to the line when i add the recognizer to my imageview. What do I wrong?
My ImageView: It works
UIImageView *live;
live = [[UIImageView alloc]initWithFrame:CGRectMake(92, 230, 136, 100)];
live.image = [UIImage imageNamed:#"online.png"];
[live addSubview:onlineLabel2];
[live setUserInteractionEnabled:YES];
[self.view addSubview:live];
[super viewDidLoad];
and my Gesture Recognizer:
UITapGestureRecognizer *singleTaP3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(onlineTap:)];
singleTaP3.numberOfTapsRequired = 1;
singleTaP3.numberOfTouchesRequired = 1;
[live addGestureRecognizer:singleTaP3];
and the last line i get the crash.
It sounds like your live view is not retain, so when you try to add the gesture, the view doesn't exist anymore. Try to record your live view as a property in your interface, and synthetise it in your implementation.
I am not sure if that this causing you to crash your code but you should call
[super viewDidLoad];
at the start of the code. (If you are using ARC than this looks fine.)
[super viewDidLoad];
UIImageView *live;
live = [[UIImageView alloc]initWithFrame:CGRectMake(92, 230, 136, 100)];
live.image = [UIImage imageNamed:#"online.png"];
[live addSubview:onlineLabel2];
[live setUserInteractionEnabled:YES];
[self.view addSubview:live];
I test the following and it works.
The interface:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) UIView *v;
- (void)tapAction;
#end
And the implementation:
#import "ViewController.h"
#implementation ViewController
#synthesize v=_v;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_v = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 50, 50)];
[_v setBackgroundColor:[UIColor redColor]];
[self.view addSubview:_v];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapAction)];
tap.numberOfTouchesRequired = 1;
tap.numberOfTapsRequired = 1;
[_v addGestureRecognizer:tap];
}
- (void)tapAction
{
NSLog(#"tap tap");
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
My problem was in my main viewcontroller. I called the new viewcontroller wrong, and get nullpointer for it. Get it in property, and it works. The problem is not with gesturetaprecognizer.
I want to drop MKAnnotationView pins on a map and have the pins be animated much like the MKUserLocation animates (the blue dot with circular waves coming out)
How do you achieve this type of animation? What steps do I need to take?
Thanks!
Animate it like any UIView!
subclass MKAnnotationView for custom content. don't go via repeated addAnnotation calls to animate it! that is wrong
you can treat the anotationView just like any other view..
here is some code that has a blinking animation of images -- this code should get you started.
(not pretty but the exact stuff you will need for your case!)
#import "DDViewController.h"
#import <MapKit/MapKit.h>
#import <QuartzCore/QuartzCore.h>
#interface DummyAnnotation : NSObject<MKAnnotation>
#end
#implementation DummyAnnotation
- (CLLocationCoordinate2D)coordinate { return CLLocationCoordinate2DMake(51, 10); }
- (NSString *)title { return #"Dummy"; }
#end
#interface DummyAnnotationView : MKAnnotationView
#end
#implementation DummyAnnotationView
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
imageView.animationImages = #[[UIImage imageNamed:#"1.gif"],[UIImage imageNamed:#"2.gif"]];
imageView.animationDuration = 5;
[imageView startAnimating];
[self addSubview:imageView];
return self;
}
#end
#interface DDViewController() <MKMapViewDelegate>
#end
#implementation DDViewController
- (MKMapView*)mapView {
return (MKMapView*)self.view;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//init a region
MKCoordinateRegion region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(51.0, 10.0), MKCoordinateSpanMake(2.0, 2.0));
[self.mapView setRegion:region animated:NO];
//add a dumy pin
DummyAnnotation *ann = [[DummyAnnotation alloc] init];
[self.mapView addAnnotation:ann];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if([annotation isKindOfClass:[DummyAnnotation class]]) {
DummyAnnotationView *view = (id)[mapView dequeueReusableAnnotationViewWithIdentifier:#"animated"];
if(!view)
view =[[DummyAnnotationView alloc ] initWithAnnotation:annotation reuseIdentifier:#"animated"];
view.bounds = CGRectMake(0, 0, 59, 59);
view.backgroundColor = [UIColor purpleColor];
//
//Animate it like any UIView!
//
CABasicAnimation *theAnimation;
//within the animation we will adjust the "opacity"
//value of the layer
theAnimation=[CABasicAnimation animationWithKeyPath:#"opacity"];
//animation lasts 0.4 seconds
theAnimation.duration=0.4;
//and it repeats forever
theAnimation.repeatCount= HUGE_VALF;
//we want a reverse animation
theAnimation.autoreverses=YES;
//justify the opacity as you like (1=fully visible, 0=unvisible)
theAnimation.fromValue=[NSNumber numberWithFloat:1.0];
theAnimation.toValue=[NSNumber numberWithFloat:0.1];
//Assign the animation to your UIImage layer and the
//animation will start immediately
[view.layer addAnimation:theAnimation forKey:#"animateOpacity"];
return view;
}
return nil;
}
//---
#end
uploaded the working sample to my dropbox (it will go away eventually but the code above is all but the image resources and the default boilerplate code of an iOS app) :: https://dl.dropbox.com/u/3753090/test.zip
After seeing some examples, I'm trying to create a window where an Image view could be moved with the PanGestureRecognizer. I don't understand what is missing.
I created and initialized an UIView object
I also created an UIGestureRecognizer for panning, for both views.
I created the method to be selected when Gesture is recognized.
If you have an idea of what is missing, I would be thankful to read it;
Here is my code:
View Controller.h
#import <UIKit/UIKit.h>
#import "bouton.h"
#interface ATSViewController : UIViewController {
boutonHome *bouton; }
#property (retain, nonatomic) boutonHome *bouton;
-(IBAction)handlePanGesture:(UIPanGestureRecognizer*)sender;
#end
View Controller.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:#"back.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
// Set self's frame to encompass the image
bouton.frame = frame;
bouton.boutonImage = image;
[self.view addSubview:bouton];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(handlePanGesture:)];
[bouton setUserInteractionEnabled:YES];
[bouton addGestureRecognizer:panGesture];
[self.view setUserInteractionEnabled:YES];
[self.view addGestureRecognizer:panGesture];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)handlePanGesture:(UIPanGestureRecognizer *)sender
{
CGPoint translate = [sender translationInView:self.view];
CGRect newFrame = bouton.frame;
newFrame.origin.x += translate.x;
newFrame.origin.y += translate.y;
sender.view.frame = newFrame;
if(sender.state == UIGestureRecognizerStateEnded)
bouton.frame = newFrame;
}
BoutonHome.h
#import <Foundation/Foundation.h>
#interface boutonHome : UIView
{
UIImage *boutonImage;
}
#property (nonatomic, retain) UIImage *boutonImage;
// Initializer for this object
#end
boutonHome.m
#import "bouton.h"
#implementation boutonHome
#synthesize boutonImage;
#end