Add ones UIScrollView Gesture recognisers to another UIScrollView - iphone

i have a point in my app where I remove one scrollview and add another. I want the same gestures in my previous scroll view to be present in the new one.
I have tried:
scrollView.gestureRecognizers = self.scrollView.gestureRecognizers;
But it just gave me a EXC_BAD_ACCESS.
Thank you!

You need to reinitialize the gesture recognizer with the new target. You can't just copy it like this.
UIGestureRecognizer *newGestureRecognizer = [UIGestureRecognizer alloc] initWithTarget:newScrollView action:#selector(yourAction);

Related

iphone - UIGestureRecognizer prevents UITableView from scrolling in Xcode 4.5

I have added two swipe gesture recognizers (swipe left and swipe right) to my UITableView. After that my table view has stopped scrolling. At the same time -(void)didSelectRowAtIndex works fine.
What might be the problem?
All I did is drag-and-dropped Swipe Gesture Recognizers from objects library onto my UITableView.
If I remove them, my table starts to scroll again.
UPD:
This happens after upgrading to Xcode 4.5. There is no such problem in older versions of Xcode.
To avoid this behaviour - add UIGestureRecognizers programatically, not in IB.
just try bellow code may this help you....
write bellow code in viewDidLoad: method
UISwipeGestureRecognizer *swipeGestureObjectImg = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(yourSlideOpen_Clicked:)] autorelease];//yourSlideOpen_Clicked is method name where you doing something
swipeGestureObjectImg.numberOfTouchesRequired = 1;
swipeGestureObjectImg.direction = (UISwipeGestureRecognizerDirectionLeft);
[yourView addGestureRecognizer:swipeGestureObjectImg];
UISwipeGestureRecognizer *swipeGestureRightObjectImg = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(yourSlideClose_Clicked:)] autorelease];//yourSlideClose_Clicked is method name where you doing something
swipeGestureRightObjectImg.numberOfTouchesRequired = 1;
swipeGestureRightObjectImg.direction = (UISwipeGestureRecognizerDirectionRight);
[yourView addGestureRecognizer:swipeGestureRightObjectImg];
if here tableview is subview of UIView class then use above code otherwise just try "youtTableview" insted of "yourView"
i hope this help you...
:)
set property of gesture recognizer cancelsTouchesInView = NO
I had the same issue:
I used to drag my TabGestureRecognizer directly onto the storyboard instead of creating them through code. With iOS6 this now seems to break scrolling.
I prefer adding views, gestures, etc directly onto the storyboard instead of through code.
You can still do this but with two changes:
1) Referencing Outlet Collections
Right-click the GestureRecognizer which is visible to you within the scene's dock.
Clear the section Referencing Outlet Collections
2) Attach the GestureRecognizer to the related view through code
Create an outlet for your GestureRecognizer
Add the following line within the viewDidLoad method:
[self.tableView addGestureRecognizer:_tabGesture];
I faced this same problem and I solved it by linking the gesture recognizer to the view instead of to the table view.
Show the storyboard.
Show the connections inspector.
Show the document outline.
Create new link at "Referencing Outlet Collections" between gesture recognizer and view node under the view controller.
Regards.
Pedro.

uilabel inside a uiview -- adding gesture to the label not owrking

I have a uilabel (and other labels as well) inside a uiview.
I added a gesture that when the label is tapped on I perform some ibaction.
The action is not triggered, however if i place the label outside the uiview (meanning it's not a child of the uiview) than it works fine. I placed it back inside the view than it's stop triggering the action.
any idea why?
something when the label is inside the uiview makes it not react to the gesture and I'm not sure why (despite the fact that both the uiview and the label have 'User interaction enabled).
TIA
Or just not have a UILabel and have only the text of the button be visible, that way you dont have to have a UILabel and a UIButton (from a memory perspective).
I think the problem here is that the UILabel is receiving the touch event instead of the UIView. Try adding the following code (modified to fit your label, of course) to the viewDidLoad method:
myLabel.userInteractionEnabled = FALSE;
This should allow touch events to pass through to the UIView. Why are you doing this? Might it not be easier to work with a UIButton?
apparently there was an issue with the parent control, I redrew the View and it worked fine. Thanks for your comments.

uiautomation - Tapping custom view doesn't work

I am trying to automate some tests of my ipad application.
I have a scrollview which contains a custom view.
The custom view overwrites the drawRect and has a TapRecognizer.
The custom view is created into the code and I have set this properties
myView.userInteractionEnabled = YES;
[myView setIsAccessibilityElement:YES];
[myView setAccessibilityLabel:#"myView"];
The custom view is added to the scrollview with
[myScrollView addSubview:myView];
Everything works smooth both on device and on simulator: tapping on the view, the tap recognizer callback is called and the custom view can draw something at the tap point.
I would automate the view test and then I need to simulate user's taps on myView.
In the uiautomation script I have something like this:
myView = circuitScrollView.elements()[0];
myView.logElement();
myView.tapWithOptions({x:56, y:576});
to simulate a user tap at position x=56 and y=576.
Nothing happens, it seems that myView doesn't receive any tap (just in case, I play a sound into the TapRecognizer but it has never been sounded).
I have tryed this too:
myView.tap();
no success.
Any idea ?
Thank you in advance.
Fab.
This may be of some use to you. I wrote a test that taps certain x/y coordinates. Rather than touching the scroll view try tapping the window as demonstrated below.
To do so I wrote:
var window = UIATarget.localTarget();
window.tap({x:x_co , y:y_co});
x_co and y_co were my coordinates.
Hope this helps.
Maybe myView = circuitScrollView.elements()["myView"] might help instead of myView = circuitScrollView.elements()[0]; ?
Maybe your custom view is not the first element in the element tree of your circuitScrollView...

Re-add a UIButton

I'm creating a simple matching game for kids for iPad. All images are drawn on buttons.
When a matching pair is found,
[matchedBtn1 removeFromSuperview];1
[matchedBtn2 removeFromSuperview];
Now, I'm creating a reset function which allows player to reset the once he/she is done.
Question is how do I re-add the UIButtons</code>?
Tried this,
[self addSubview:matchBtn1];
But the program crashes once reset button is clicked and gives this warning for every button I addSubview to
"viewController" may not respond to "-addSubview".
Many thanks in advance for your help. :)
try :
[self.view addSubview:matchBtn1];
I think you should be trying to add them to the view of the controller, not the view controller itself.
[[viewController view] addSubView:matchBtn1]; 
Also make sure you have kept a retained copy of matchBtn1 somewhere in your classes as it will have been released when you removed the subviews.
Instead of adding and removing them, create a set of transparent views over the buttons and when you want to remove them, change the color of those views so that the buttons are hidden. That way if you want to reset the screen, you just have to reset all those views to transparent. You may have to set the cover views to intercept touches too, so the user doesn't click hidden buttons.

Can't touch UITextField on UIScrollView

I know this has been talked about a lot. I think I've gone thru every question on this site, and still have not been able to get this working.
I'm new to developing but I have a good sense of what's going on with all of my code. I definitely don't have a lot of experience though, this is my first iPhone app.
I'm making a data entry field that is comprised of multiple UITextFields within a UIScrollView. I'll avoid explaining the other details for now, as it seems its a very basic problem. Without a scrollview, the textfields work perfectly. I can touch the textfield and the keyboard or picker view show up appropriately. When I add the textfields to a scrollview, the scrollview works, but then the text fields don't receive my touches.
Here's the key: When 'User Interaction' is ENABLED, the scrollview works but the textfield touches are NOT registered. When 'User Interaction' is DISABLED, the scrollview doesn't work, but the textfield touches ARE registered and the keyboard/picker pops up.
From the other posts I have seen people creating subclasses and overriding the touches in a separate implementation. I've seen people using custom content views (subviews?), I've seen some solutions that are now obsolete since the APIs have changed in newer versions of the SDK, and I am just completely stuck.
I will leave out my code for now, because maybe there is a solution that someone has without requiring my code. If someone needs to see my code, I will put it up. My app is being written in the 3.1.3 SDK.
If anyone has ANY information that could help, it would be so greatly appreciated.
Here is what worked for me in Xcode 4.3.3.
In the storyboard, select your scrollview. Select Attribute Inspector on the right side. Uncheck Delays Content Touches.
It sounds like you're using IB to do a lot of your UI layout. If you take a programmatic approach you could set-up the following view hierarchy which should work.
Your view controller object managing your scroll view and your text fields should have a UIScrollView object and a UIView object (in addition to any UITextField objects you need). In the loadView method of the view controller class, allocate and initalize the UIView object and add your text fields to it as subviews. At the end of the method, allocate and initalize the UIScrollView object then add the UIView to the UIScrollView as a subview.
As an example, if your UIScrollView object were called scrollView and your UIView object called mainView the following lines at the end of the view controller's loadView method would properly set up the scroll view with the main view with the text fields on it:
scrollView = [[UIScrollView alloc] initWithFrame: [[UIScreen mainScreen] bounds] ];
scrollView.backgroundColor = [UIColor whiteColor];
scrollView.contentSize = mainView.frame.size;
[scrollView addSubview: mainView];
self.view = scrollView;
You may need to enable / disable user interaction for both the scroll view and text fields as necessary.
If possible you might best using different touch mechanisms for each process (scrolling and textfield input).
Maybe you can check the time differential between touch start and end such that you can detect the difference between a drag (scroll) and a tap (text input).
Having said that if you propagate the touches up from the textfields to the scrollview you should be fine. One thing I use is:
[myObject addTarget:self action:#selector(itemTouchedUpInside:) forControlEvents:UIControlEventTouchUpInside];
which passes the object touched so you can identify it and act accordingly.