touch began in UIScrollView - iphone

touchesBegan: doesn't work in UIScrollView. How do I enable touchesBegan: in UIScrollView?

You have to subclass UIScrollView to allow interception of the touch events.
For example, you could create a "special" UISCrollView like this:
#interface TouchableScrollView : UIScrollView {
}
#end
.m file:
#implementation TouchableScrollView
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {
if (!self.dragging) {
[self.nextResponder touchesEnded: touches withEvent:event];
}
[super touchesEnded: touches withEvent: event];
}
#end
This example only overrides touchesEnded, but you can do the same for touchesBegan

I assume that u are seeking for code to scroll down and hide the scroll down Img. so this is the code.
// scroll down image show and hidder.
- (void)scrollViewDidScroll:(UIScrollView *)myScrollView{
//NSLog(#"%lf",myScrollView.contentOffset.y);
if (myScrollView.contentOffset.y > 220 )
scrollDownImg.hidden = YES ;
else
scrollDownImg.hidden = NO ;
}
U may get the scrolled location by using the myScrollView.contentOffset

Related

How do I apply a UILongPressGestureRecognizer to a shape in C4?

I am working in C4 and want to be able to press down on a shape and drag it at a slow speed upward.
I have been working with the "Getting the UITouch objects for a UIGestureRecognizer" tutorial and other TouchesMoved and TouchesBegan Objective-C tutorials but the gesture is not applying to the shape. The project builds but the shape still does not move when you press and drag it. Is there a particular way C4 applies gestures to shapes?
This is my code:
DragGestureRecognizer.h
#import <UIKit/UIKit.h>
#interface DragGestureRecognizer : UILongPressGestureRecognizer {
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
#end
#protocol DragGestureRecognizerDelegate <UIGestureRecognizerDelegate>
- (void) gestureRecognizer:(UIGestureRecognizer *)gr movedWithTouches:(NSSet*)touches andEvent:(UIEvent *)event;
#end
C4WorkSpace.m:
#import "C4WorkSpace.h"
#import "DragGestureRecognizer.h"
#implementation DragGestureRecognizer
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
if ([self.delegate respondsToSelector:#selector(gestureRecognizer:movedWithTouches:andEvent:)]) {
[(id)self.delegate gestureRecognizer:self movedWithTouches:touches andEvent:event];
}
}
#end
#implementation C4WorkSpace {
C4Shape *circle;
}
- (void)longPress:(UILongPressGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateEnded ) {
NSLog(#"Long Press");
}
}
-(void)setup {
circle = [C4Shape ellipse:CGRectMake(5,412,75,75)];
[self.canvas addShape:circle];
}
#end
C4 has a method that simplifies the process of adding gestures to any visible object (e.g. C4Shape, C4Movie, C4Label...).
NOTE: you will need to create a subclass of C4Shape in which you can create some custom methods.
For instance, the following will add a tap gesture to an C4Shape:
C4Shape *s = [C4Shape rect:CGRectMake(..)];
[s addGesture:TAP name:#"singleTapGesture" action:#"tap"];
The addGesture:name:action: method is listed in the C4Control documentation, and is defined like:
Adds a gesture to an object.
- (void)addGesture:(C4GestureType)type name:(NSString *)gestureName action:(NSString *)methodName
The (C4GestureType)type can be any one of:
TAP
SWIPERIGHT
SWIPELEFT
SWIPEUP
SWIPEDOWN
PAN
LONGPRESS
The following gestures are available, but haven't yet been tested:
PINCH
ROTATION
The (NSString *)gestureName allows you to specify a unique name for the gesture.
The (NSString *)methodName allows you to specify the method that the gesture will trigger.
So... getting back to the example above:
[s addGesture:TAP name:#"singleTapGesture" action:#"tap"];
...will add to the s object a TAP gesture called singleTapGesture which, when triggered, will run a -(void)tap; method (which needs to have already defined in your class).
You can apply the technique I described above, but instead use:
[s addGesture:PAN name:#"panGesture" action:#"move:];
you will also have to define a
-(void)move:(id)sender;
method in your shape's class.

detect touchesEnded in a scrollview

so I have a scroll view and what I would like is that when I end my touch (touchesEnded) in this scroll view : (do something) but it doesn't detect my touchesended I don't know why. How can I solve this please .sorry for my english I'm french :/
Subclass uiScrollview like this and it should work:
In .h file:
#interface MyScrollView : UIScrollView {
}
#end
In .m file:
#implementation MyScrollView
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {
if (!self.dragging) {
[self.nextResponder touchesEnded: touches withEvent:event];
}
[super touchesEnded: touches withEvent: event];
}
#end

UIScrollView inside UITableViewCell touch detect

I have a tableview with 8 custom cells. in the 8th cell I added a scrollView with paging enabled so I can show page 1 and page 2 (or 3, 4... 10) without have a very high cell.
The problem is with the scrollView I can't use didSelectRowAtIndexPath because the cell is behind the scrollView so I'm trying to detect scrollView tap (not swipe).
I played with touchesBegan and touchesEnded but they are never called (I know touches work with UIView only, but maybe.....)
Any help is very appreciated.
Thanks,
Max
There is a trick Apple recommends to use in this case, in theirs WWDC 2014 session "Advanced scrollviews" (See Demo starting from 8:10):
[cell.contentView addSubview:_scrollView];
[_scrollView setUserInteractionEnabled:NO];
[cell.contentView addGestureRecognizer:_scrollView.panGestureRecognizer];
That's all what needs to be done, no need to override touchesBegan:, touchesMoved: and others.
I used solution based on overriding of touchesBegan:, touchesMoved:, touchesEnded: and touchesCancelled: previously, but sometimes it caused a weird behaviour: when select a certain cell, method -tableView:didSelectRowAtIndexPath: was called for cell with different indexPath.
Solution from Apple has no side effects so far and looks more elegant.
There is also an elegant resolution:
Create a SubClass from UIScrollView and override the following methods
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[self superview]touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[[self superview]touchesMoved:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[[self superview]touchesCancelled:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[[self superview]touchesEnded:touches withEvent:event];
}
Passing every touch to the superview of the scroll view and then the didSelectRowAtIndexPath will be called.
Solved subclassing both uitableviewcell and uiscrollview.
It worked for my needs. Hope it can help.
Max
myScrollView.h
#import <UIKit/UIKit.h>
#interface myScrollView : UIScrollView {
}
#end
myScrollView.m
#import "myScrollView.h"
#implementation myScrollView
- (id)initWithFrame:(CGRect)frame {
return [super initWithFrame:frame];
}
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
NSLog(#"touch scroll");
// If not dragging, send event to next responder
if (!self.dragging)
[self.nextResponder touchesEnded: touches withEvent:event];
else
[super touchesEnded: touches withEvent: event];
}
myCell.h
#import <UIKit/UIKit.h>
#interface myCell : UITableViewCell {
}
#end
myCell.m
#import "myCell.h"
#implementation myCell
- (id)initWithFrame:(CGRect)frame {
return [super initWithFrame:frame];
}
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event
{
NSLog(#"touch cell");
// If not dragging, send event to next responder
[super touchesEnded: touches withEvent: event];
}
RootViewController.h
#import <UIKit/UIKit.h>
#class myCell;
#class myScrollView;
#interface RootViewController : UITableViewController {
myCell *cell;
myScrollView *scrollView;
}
#end
RootViewController.m
#pragma mark -
#pragma mark Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
// my custom cell
cell = [[myCell alloc] init];
if (cell == nil) {
cell = [[[myCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// the custom scroll view
scrollView = [[myScrollView alloc] initWithFrame:cell.frame];
scrollView.contentSize = CGSizeMake(640, 40);
[cell.contentView addSubview:scrollView];
//something to add in scrollView
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 20)];
label.text = #"some text";
[scrollView addSubview:label];
// Configure the cell.
return cell;
}
The selected answer is correct, but I updated the code based on a bug I was getting.
In the subclassed scroll view add the following code.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (self.dragging) {
[super touchesMoved:touches withEvent:event];
} else {
if ([self.delegate isKindOfClass:[UITableViewCell class]]) {
[(UITableViewCell *)self.delegate touchesCancelled:touches withEvent:event];
}
[self.superview touchesMoved:touches withEvent:event];
}
}
If your self.delegate is not the UITableViewCell, than replace that property with a property to your cell.
The cell needs to retrieve the cancel touch event during movement to prevent the undesired results. It can be easily reproducible as follows.
Highlight the cell (assuming the scroll view is over the whole cell, if not highlight the scroll view)
While the cell is highlighted, drag the table view
Select any other cell and now the previously highlighted cell will retrieve the didSelectCell state
Another point to mention is that order matters! If the self.delegate is not called before the self.superview then the highlighted state wont happen.
I found the simplest solution for my needs:
subclass UIScrollView touchesEnded method and post a notification.
In the UITableview add an observer in viewdidAppear (remove it in viewdiddisappear) to call a function that call tableview didSelectRowForIndexPath.
Something like this (swift version)
// myScrollView.swift
import UIKit
class myScrollView: UIScrollView {
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
NSNotificationCenter.defaultCenter().postNotificationName("selectTVRow", object: nil)
}
}
In your tableView:
// ItemsList.swift
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "selectFourthRow", name: "selectTVRow", object: nil)
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self, name: "selectfourthrow", object: nil)
}
func selectFourthRow() {
let rowToSelect:NSIndexPath = NSIndexPath(forRow: 4, inSection: 0);
self.tableView(self.tableView, didSelectRowAtIndexPath: rowToSelect);
}
/*
.... rest of your tableview Datasource and Delegate methods...
numberOfSectionsInTableView, numberOfRowsInSection, cellForRowAtIndexPath
*/

How to setup UIView touch handler without subclassing

How do I capture touch events such as - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event without subclassing a UIView nor using UIViewControllers.
What happens is that I have a simple UIView created programmatically and I need to detect basic tap events.
If you are writing your app for iOS 4, use UIGestureRecognizer. You can then do what you want. Recognize gestures without subclassing.
Otherwise, subclassing is the way to go.
There's just no reason not to. If you subclass and add nothing it's just a UIView called by another name. All you are doing is intercepting those functions that you are interested in. Don't forget you can do [super touchesBegan:touches] inside your subclass' touchesBegan if you don't want to stop responders up the chain from getting those events too.
I don't why you don't want to use the normal method of subclassing a UIView to capture touch events, but if you really need to do something weird or sneaky, you can capture all events (including touch events) before they get sent down the view hierarchy by trapping/handling the sendEvent: method at the UIWindow level.
CustomGestureRecognizer.h
#import <UIKit/UIKit.h>
#interface CustomGestureRecognizer : UIGestureRecognizer
{
}
- (id)initWithTarget:(id)target;
#end
CustomGestureRecognizer.mm
#import "CustomGestureRecognizer.h"
#import <UIKit/UIGestureRecognizerSubclass.h>
#interface CustomGestureRecognizer()
{
}
#property (nonatomic, assign) id target;
#end
#implementation CustomGestureRecognizer
- (id)initWithTarget:(id)target
{
if (self = [super initWithTarget:target action:Nil]) {
self.target = target;
}
return self;
}
- (void)reset
{
[super reset];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
[self.target touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
[self.target touchesMoved:touches withEvent:event];
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent: event];
[self.target touchesEnded:touches withEvent:event];
}
#end
Usage:
CustomGestureRecognizer *customGestureRecognizer = [[CustomGestureRecognizer alloc] initWithTarget:self];
[glView addGestureRecognizer:customGestureRecognizer];

Double Tap inside UIScrollView to Another View

I want to preface this question with the fact I am new to the iphone application development and many times believe I may be over my head. I am trying to find an action that will allow me to double tap anywhere within my full screen UIScrollView to return to my main menu (MainMenuViewController).
I currently have the following code running for my ScrollViewController...
ScrollViewController.h
#import <UIKit/UIKit.h>
#interface ScrollViewController : UIViewController <UIScrollViewDelegate>
{
}
#end
ScrollViewController.m
#import "ScrollViewController.h"
UIScrollView *myScrollView;
UIPageControl *myPageControl;
#implementation ScrollViewController
- (void)loadScrollViewWithPage:(UIView *)page
{
int pageCount = [[myScrollView subviews] count];
CGRect bounds = myScrollView.bounds;
bounds.origin.x = bounds.size.width * pageCount;
bounds.origin.y = 0;
page.frame = bounds;
[myScrollView addSubview:page];
}
...etc
Any advice or sample code to implement a double tap within the ScrollView Controller to allow me to return to my MainMenuViewController would be greatly appreciated. Also please include any Interface Builder changes to the view (if necessary). I have been pouring over the forums for days and have not been able to successfully implement this action. Thanks...R.J.
First of all, create a subclass of a UIScrollView:
#import <UIKit/UIKit.h>
#interface MyScrollView : UIScrollView {
}
#end
Implement it:
#import "MyScrollView.h"
#implementation MyScrollView
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {
if (!self.dragging) {
[self.nextResponder touchesEnded: touches withEvent:event];
}
[super touchesEnded: touches withEvent: event];
}
- (void)dealloc {
[super dealloc];
}
#end
Then, use this subclass instead of a normal UIScrollView in your main View Controller. This will call the touchesEnded:withEvent: method (alternatively, you can call any method you want). Then, track for double taps (if you need info on how to do that, let me know).