I have made an animation that moves across the screen, my animation loops continuously. How can I stop the animation when you tap the animated image, Then let the animation continue when you lift the touch?
I know how to use TouchesMoved to move a specified button like this:
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
UIControl *control = sender;
control.center = point;
But getting it to work with my animation. I would like the animation to continue after I touch it.
SelectedCellViewController.h
// SelectedCellViewController.h
#import <Accounts/Accounts.h>
#import <QuartzCore/QuartzCore.h>
#interface SelectedCellViewController : UIViewController {
IBOutlet UIImageView *imageView;
UIImageView *rocket;
}
#end
viewControllertoShow.m
#import "SelectedCellViewController.h"
#interface SelectedCellViewController ()
#end
#implementation SelectedCellViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
}
return self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:#selector(imageSpawn:) withObject:nil afterDelay:3];
}
- (void) imageSpawn:(id) sender
{
UIImage* image = [UIImage imageNamed:#"ae"];
rocket = [[UIImageView alloc] initWithImage:image];
rocket.frame = CGRectMake(-25, 200, 25, 40);
[UIView animateWithDuration:5
delay:0.2f
options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
animations:^(){rocket.frame=CGRectMake(345, 200, 25, 40);}
completion:^(BOOL fin) {
}];
[self.view addSubview:rocket];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(ballTapped:)];
tapped.numberOfTapsRequired = 1;
[rocket addGestureRecognizer:tapped];
[rocket setUserInteractionEnabled:YES];
}
-(void)ballTapped:(UIGestureRecognizer *)gesture
{
CGPoint location = [gesture locationInView:gesture.view];
//then write code to remove the animation
[self.view.layer removeAllAnimations];
NSLog(#"Tag = %d", gesture.view.tag);
rocket.frame = CGRectMake(location.x,location.y,25,40);
}
- (void)dismissView {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidUnload {
}
#end
As you had already stated in your question ,you can get the touched point using
CGPoint point = [[[event allTouches] anyObject] locationInView:self.view];
Then check check whether this point lies inside the coordinates of the animated UIImageview and then stop animation.
But if you are using scrollview, you won't be able to use this because scrollview will not return any UIView touch events.
As your image view is animating, a better choice will be adding a UITapGestureRecogniser to the image view when you add it s subview, like this
- (void) imageSpawn:(id) sender
{
UIImage* image = [UIImage imageNamed:#"ae"];
UIImageView *rocket = [[UIImageView alloc] initWithImage:image];
rocket.frame = CGRectMake(-25, 200, 25, 40);
[UIView animateWithDuration:5
delay:0.2f
options:UIViewAnimationCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse
animations:^(){rocket.frame=CGRectMake(345, 200, 25, 40);}
completion:^(BOOL fin) {
}];
[myScrollView addSubview:rocket];
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(ballTapped:)];
tapped.numberOfTapsRequired = 1;
[self.rocket addGestureRecognizer:tapped];
[rocket setUserInteractionEnabled:YES];
}
And in the target function write code to stop the animation:
-(void)ballTapped:(UIGestureRecognizer *)gesture
{
//here also you can get the tapped point if you need
CGPoint location = [gesture locationInView:gesture.view];
//then write code to remove the animation
[self.view.layer removeAllAnimations];
}
EDIT:
If you are trying to stop the image view at the touched point, you can add this to ballTapped event:
rocket.frame = CGRectMake(location.x,location.y,25,40);
But for this you have to declare UIImageView *rocket outside this particular method, i.e. declare to in your header file.
Another way is to add this to the parent view of the animation -
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
self.animating = NO;
return [super hitTest:point withEvent:event];
}
use an (atomic) property, then check the property in the animation to see if it should be stopped. We use this to stop a photo gallery which is running so that the user can manually move the photos. You could also check in here if the point is in a specific area if necessary. This method runs before any touch methods are called.
try like this may be it helps you, in the middle of the animation if you touch on the imageview then animation removes from the view .
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint lastLocation = [touch locationInView: self.view];
if([[touch view] isKindOfClass:[UIImageView class]]){
[youImageView.layer removeAllAnimations];
youImageView.center=lastLocation;//assign your image view center when the animation removes from the view.
}
}
and add #import <QuartzCore/QuartzCore.h> framework.
Related
I'm trying to add touch events to small view blocks drifting from screen right side to left side. The problem is layer.presentationLayer hitTest:point method returns nothing, even if I do actually tap within the bounds of the view block.
Finally, I find a workaround. But, two questions still confuse me.
The converted point does not seem to be right, how to properly use presentationLayer hitTest?
In my code snippet, UIViewAnimationOptionAllowUserInteraction is not set, why I can handle touch event anyway?
The following are the code, any help will be appreciated
viewDidLoad
CGRect bounds = [[UIScreen mainScreen] bounds];
for (int i = 0; i < 15; i++) {
ViewBlock *view = [[ViewBlock alloc] initWithFrame:CGRectMake(bounds.size.width, 200, 40, 40)];
[self.view addSubview:view];
[UIView animateWithDuration:20 delay:i * 21 options:UIViewAnimationOptionCurveLinear animations:^{
view.frame = CGRectMake(-40, 200, 40, 40);
} completion:^(BOOL finished) {
[view removeFromSuperview];
}];
}
ViewBlock.m
#implementation ViewBlock
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
self.backgroundColor = [UIColor redColor];
}
return self;
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
CALayer *presentationLayer = self.layer.presentationLayer; //Do NOT modify presentationLayer
if (presentationLayer) {
CGPoint layer_point = [presentationLayer convertPoint:point fromLayer:self.layer.modelLayer];
if ([presentationLayer hitTest:point]) {
return self; //not worked
}
if ([presentationLayer hitTest:layer_point]) {
return self; //not worked either
}
if (CGRectContainsPoint(presentationLayer.bounds, layer_point)) {
return self; //the workaround
}
}
return [super hitTest:point withEvent:event];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[super touchesBegan:touches withEvent:event];
NSLog(#"touches began");
}
- (void)didMoveToSuperview{
[super didMoveToSuperview];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapTouched:)]
;
tap.cancelsTouchesInView = NO;
[self addGestureRecognizer:tap];
}
- (void)tapTouched:(UITapGestureRecognizer *)sender{
NSLog(#"touches gesture");
}
#end
Q1: The converted point does not seem to be right, how to properly use presentationLayer hitTest?
What you are missing is the parameter of hitTest: should be in the coordinate system of the receiver's super layer, so the code should be:
CGPoint superLayerP = [presentationLayer.superlayer convertPoint:layer_point fromLayer:presentationLayer];
if ([presentationLayer hitTest:superLayerP]) {
return self;
}
Q2:In my code snippet, UIViewAnimationOptionAllowUserInteraction is not set, why I can handle touch event anyway
I think you can get touch event because you override - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event and return self as result, so the event will pass to it. By default(If you don't override the hitTest:withEvent: method), you won't get touch event.
If you set UIViewAnimationOptionAllowUserInteraction option, you can get touch event on the view's final frame (CGRectMake(-40, 200, 40, 40)), but in this example, the final frame is off the screen, you can set it to CGRectMake(0, 200, 40, 40) and have a test.
Ok, so I have been looking forever on how to use UITouchGestures with UIImageViews to remove the View, but everything either didn't make sense or didn't work so I finally decided to ask the question. How do you use A UITapGestureRecognizer to remove a UIImageView when it is tapped?
This is my .m:
- (void)viewDidLoad
{
[super viewDidLoad];
//this makes a zombie/person
UIImageView *ZombieView =[[UIImageView alloc] initWithFrame: CGRectMake(135 , -100, 50, 75)];
UIImage *Zombie=[UIImage imageNamed:#"free-vector-stick-figure-clip-art_105575_Stick_Figure_clip_art_hight.png"];
[ZombieView setImage:Zombie];
//[self.view addSubview:ZombieView];
[self.view insertSubview:ZombieView belowSubview:_Railing];
[ZombieView setUserInteractionEnabled:TRUE];
// double tap gesture recognizer
UITapGestureRecognizer *Touch2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped2:)];
[Touch2 setDelegate: self];
[Touch2 setNumberOfTapsRequired: 2];
[_ZombieView addGestureRecognizer:Touch2];
UITapGestureRecognizer *Touch = [[UITapGestureRecognizer alloc] initWithTarget:ZombieView action:#selector(tapped:)];
[Touch setDelegate:self];
[Touch setNumberOfTapsRequired: 1];
[Touch requireGestureRecognizerToFail: Touch2];
[_ZombieView addGestureRecognizer:Touch];
//This makes the Person move down until he is behind the railing
[UIView animateWithDuration:7.5
delay:0.0
options: UIViewAnimationOptionCurveLinear
animations:^{
CGAffineTransform trans = CGAffineTransformTranslate(ZombieView.transform, 0, 420);
ZombieView.transform=trans;
}
completion:nil];}
-(void) tapped:(UITapGestureRecognizer *)recognizer{
[_ZombieView removeFromSuperview];
}
-(void) tapped2:(UITapGestureRecognizer *)recognizer{
}
-(void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I know it must be a disaster, but I am fairly new to this so please keep this in mind. I just need know how to use the Tap Recognizers and how to delete a UIImageView, but other tips are appreciated. Thank you!
This is my edited code:
#interface AbcViewController ()
#end
#implementation AbcViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//this makes a zombie/person
UIImageView *zombieView =[[UIImageView alloc] initWithFrame: CGRectMake(135 , -100, 50, 75)];
UIImage *zombie=[UIImage imageNamed:#"free-vector-stick-figure-clip-art_105575_Stick_Figure_clip_art_hight.png"];
[zombieView setImage:zombie];
//[self.view addSubview:zombieView];
[self.view insertSubview:zombieView belowSubview:_Railing];
[zombieView setUserInteractionEnabled:TRUE];
//This makes the Person move down until he is behind the railing
[UIView animateWithDuration:7.5
delay:0.0
options: UIViewAnimationOptionCurveLinear
animations:^{
CGAffineTransform trans = CGAffineTransformTranslate(zombieView.transform, 0, 420);
zombieView.transform=trans;
}
completion:nil];
// double tap gesture recognizer
UITapGestureRecognizer *touch2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tapped2:)];
[touch2 setDelegate: self];
[touch2 setNumberOfTapsRequired: 2];
[zombieView addGestureRecognizer:touch2];
UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:zombieView action:#selector(tapped:)];
[touch setDelegate:self];
[touch setNumberOfTapsRequired: 1];
[touch requireGestureRecognizerToFail: touch2];
[zombieView addGestureRecognizer:touch];
}
-(void) tapped:(UITapGestureRecognizer *)recognizer{
[recognizer.view removeFromSuperview];
}
-(void) tapped2:(UITapGestureRecognizer *)recognizer{
[recognizer.view removeFromSuperview];
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
A tap gesture recognizer has a view property that points to the view it's attached to. So, in the action method for the recognizer, use it to remove itself from its superview,
-(void) tapped:(UITapGestureRecognizer *)recognizer{
[recognizer.view removeFromSuperview];
}
It's hard to tell why your method isn't working. It could be because _ZombieView is nil (I don't see anywhere you actually create _ZombieView as opposed to ZombieView which you do create). BTW, you should start your property and variable names with a lowercase letter to conform to the usual objective-c naming conventions.
I have a viewController which contains a UITableView. This viewController implements UITableViewDelegate, and UITableViewDataSource, and I'm also trying to implement the following methods:
touchesBegan
touchesMoved
touchesEnded
However, these methods are not being called. I am trying to call these methods in response to the user touching a UIImageView that is inside a UITableView, but doing so only calls this method:
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.view bringSubviewToFront:_imageView];
[UIView animateWithDuration:.3 animations:^{
CGRect rect = [self.view convertRect:[tableView rectForRowAtIndexPath:indexPath] fromView:tableView];
CGFloat floatx = _imageView.frame.origin.x - rect.origin.x;
_imageView.frame = CGRectMake(rect.origin.x + floatx, rect.origin.y, _imageView.frame.size.width, _imageView.frame.size.height);
}];
NSLog(#"Table row %d has been tapped", indexPath.row);
}
What I am hoping is to continue to call this method, but also call:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == _imageView) {
//need to make sure user can only move the UIImageView up or down
CGPoint location = [touch locationInView:_imageView];
//ensure UIImageView does not move outside UIITableView
CGPoint pt = [[touches anyObject] locationInView:_imageView];
CGRect frame = [_imageView frame];
//Only want to move the UIImageView up or down, not sideways at all
frame.origin.y += pt.y - _startLocation.y;
[_imageView setFrame: frame];
_imageView.center = location;
return;
}
}
when the user is dragging the UIImageView inside the UITableView.
Can anyone see what I am doing wrong?
Thanks in advance to all who reply
I had a similar problem to this except I was trying to implement touch detection to an UIWebView. I eventually got over it by adding a UIPanGestureRecognizer to the UIWebView's .view property.
Implement this method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
and then add the UIPanGestureRecognizer to the UITableView like so:
UIPanGestureRecognizer *panGesture;
panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:#selector(panGestureDetected:)];
panGesture.maximumNumberOfTouches = 1;
panGesture.minimumNumberOfTouches = 1;
panGesture.delegate = self;
[self.tableView addGestureRecognizer:panGesture];
and then implement the method
- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer {
}
Now, every time the UIPanGestureRecognizer detects a pan gesture, it will call that method and you can get the location of the UIPanGestureRecognizer by calling the method [recognizer locationInView:self.tableView.view]; which will return a CGPoint. You can also get the state of the UIPanGestureRecognizer by calling [recognizer state] which returns a UIGestureRecognizerState.
Have you set userInteractionEnabled on your view that the touches will be inside?
Somebody help!
Here is what I want to implement :
While an UIImageView is becoming alpha 0 (hidden)
it can be touched
so that its alpha becomes one (unhidden).
But UIImageView is not touched while it is animating (=becoming alpha 0).
I tried hundred skills at stackoverflow, but didn't work.
They are.......
UIViewAnimationOptionAllowUserInteraction
setUserInteractionEnabled:YES;
touchesBegan
GestureRecognizer options
etc..
Only function 'hitTest' worked but not during animation.
Please reply . Thank you.
Below is my codes.
#import "ViewController.h"
#define AD #"text.png"
#import <QuartzCore/QuartzCore.h>
#interface ViewController ()
#end
#implementation ViewController
#synthesize scrollData=_scrollData;
#synthesize adImage=_adImage;
- (void)viewDidLoad
{
//_adImage is UIImageView
_adImage=[[adImage alloc] initWithImage:[UIImage imageNamed:AD]];
_scrollData.scrollView.delegate = self;
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(singleTapGestureCaptured:)];
[_adImage addGestureRecognizer:singleTap];
[_adImage setMultipleTouchEnabled:YES];
[_adImage setUserInteractionEnabled:YES];
[self.view addSubview:_adImage];
_adImage.alpha=0;
[_adImage setUp_pt:CGPointMake(160,250)];
_adImage.center=_adImage.up_pt;
[super viewDidLoad];
[self hideImage:_adImage delay:0];
[self becomeFirstResponder];
}
- (void)hideImageComplete:(UIView*)v
{
[self hideImage:v delay:0];
}
- (void)hideImage:(UIImageView*)v delay:(int)nDelay
{
[_adImage becomeFirstResponder];
[UIView animateWithDuration:1
delay:nDelay
options:
(UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionAllowUserInteraction)
animations: ^
{
_adImage.alpha=0.0f;
}
completion:^(BOOL completed){
[self hideImageComplete:v];
}];
}
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
NSLog(#"Gesture event on view");
_adImage.alpha=1;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"hit!!");
[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self.view];
CGPoint touchLocation = [touch locationInView:self.view];
_adImage.alpha=1;
}
#end
#implementation adImage
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint touchPoint = [touch locationInView:self];
if ([[[self layer] presentationLayer] hitTest:touchPoint]) {
[self.layer removeAllAnimations];
}
}
-(UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
if ([[self.layer presentationLayer] hitTest:point]) {
NSLog(#"hit!!");
self.alpha=1;
return self;
}
return [super hitTest:point withEvent:event];
}
#synthesize up_pt;
#end
Here is my ViewController.h codes.
#import <UIKit/UIKit.h>
#interface adImage : UIImageView {
}
//#property (strong, nonatomic) UIImageView *imageView;
#property (assign)CGPoint up_pt;
#end
#interface ViewController : UIViewController <UIScrollViewDelegate>{
}
- (void)hideImageComplete:(UIView*)v;
- (void)hideImage:(UIView*)v delay:(int)nDelay;
#property (strong, nonatomic) adImage *adImage;
#property(nonatomic, strong) IBOutlet UIWebView *scrollData;
#end
I have the answer for you, but first:
always name a class with a starting capital letter, ie AdImageView (its an image view not a pure view subclass too)
I took your code but commented out all your touch methods, which you may or may not need
the root issue here is that a UIGestureRecognizer won't work if alpha is 0, so you will see the animation to alpha=0 is broken into two pieces, almost to 0, then to 0. If the user taps to cancel, then the final completion block returns the view to 0
Code:
- (void)hideImage:(UIImageView*)v delay:(int)nDelay
{
//[_adImage becomeFirstResponder];
[UIView animateWithDuration:(3.0f - 0.1f)
delay:nDelay
options: (UIViewAnimationOptionCurveEaseIn |
UIViewAnimationOptionAllowUserInteraction)
animations: ^
{
_adImage.alpha=0.1f;
}
completion:^(BOOL completed)
{
NSLog(#"completed=%d", completed);
if(completed) {
[UIView animateWithDuration:0.1f animations:^{
_adImage.alpha=0.0f;
}];
} else {
_adImage.alpha=1;
}
}];
}
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
NSLog(#"Gesture event on view");
[_adImage.layer removeAllAnimations];
}
This worked great for me on iOS7.1+:
self.viewWithTapgesture.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.01];
I have just started working on a project and I already have a few problems and errors. I was wondering if you guys could help me figure out where the bug is.
Quick overview: I have two files: (ViewController.m and Scroller.m). In the ViewController I have some code under ViewDidLoad (which I'll show in a second), I also have a function (addImagesToView) that does what it says, and some touchesBegan,moved and ended for intractability.
In the Scroller I decided to rewrite some of the "-(void)touches" functions implementations.
The problem I have: is that the buttons (coming from the addImagesToView function) get stuck on highlighted. I performed some tests with an NSLog to see which "touches" working and which don't. "touchesMoved" doesn't work properly. If the user drags downward, the log stops after about 3 or 4 lines of text. I think that it interferes with the scroll somehow.
This is the content of ViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"bgPNG.png"]];
imageView.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:imageView];
//UIColor *background = [[UIColor alloc]initWithPatternImage:imageView.image];
//self.view.backgroundColor = background;
CGRect fullScreen = [[UIScreen mainScreen] applicationFrame];
scroll = [[Scroller alloc] initWithFrame:fullScreen];
scroll.contentSize = CGSizeMake(320, 2730);
scroll.delaysContentTouches = NO;
//scroll.canCancelContentTouches = NO;
scroll.scrollEnabled = YES;
[self.view addSubview:scroll];
buttons = [[NSMutableArray alloc]init];
[self addImagesToView];
}
-(void) addImagesToView{
CGFloat yCoordinate = 35;
for (int i=1; i<=18; i++) {
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"picture%d.png",i]]highlightedImage:[UIImage imageNamed:[NSString stringWithFormat:#"picture%dHG.png",i]]];
CGRect position = CGRectMake(105, yCoordinate, IMAGE_SIZE, IMAGE_SIZE);
image.frame = position;
[scroll addSubview:image];
image.userInteractionEnabled = YES;
image.tag = i;
[buttons addObject:image];
yCoordinate += 150;
}
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Get any touch
UITouch *t = [touches anyObject];
if ([t.view class] == [UIImageView class])
{
// Get the tappedImageView
UIImageView *tappedImageView = (UIImageView*) t.view;
tappedImageView.highlighted = YES;
}
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *t = [touches anyObject];
if ([t.view class] == [UIImageView class])
{
// Get the tappedImageView
UIImageView *tappedImageView = (UIImageView*) t.view;
tappedImageView.highlighted = NO;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources th at can be recreated.
}
#end
and this is the Scroller.m
#import "Scroller.h"
#implementation Scroller
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!self.dragging)
[self.nextResponder touchesBegan: touches withEvent:event];
else
[super touchesBegan: touches withEvent: event];
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *t = [touches anyObject];
if ([t.view class] == [UIImageView class])
{
// Get the tappedImageView
UIImageView *tappedImageView = (UIImageView*) t.view;
tappedImageView.highlighted = NO;
}
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!self.dragging)
[self.nextResponder touchesEnded: touches withEvent:event];
else
[super touchesEnded: touches withEvent: event]; // Get the tappedImageView
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
I have also tried to implement all the touches in the ViewController, but I don't know how to make it read from the scroll (please notice the diff. between SCROLL and SCROLLER).
As you can tell I've tried to order them like this: view -> backGroundImage ->scroller ->addImagesToView(function), so that the images that come out from [I]addImagesToView[/I] are on top of the scroll. That's my desired hierarchy.
Thank you very much.
You don't have to write your own touch handling routines for button presses. Instead of using UIImageViews, just create UIButtons instead and hook into their UIControlEventTouchUpInside event.
I think you'd be better off telling us what you are trying to accomplish as you're probably barking up the wrong tree here. As Jim said, there are better ways to select images, and even if you want to handle touches yourself, UIGestureRecognizers are probably a better option. If you are allowing users to select images, I'd recommend you take a look at iCarousel on github. It's an open source carousel that works great for image selection. Also, if you can target iOS 6 there is a new collection view that could be right up your alley.