Detect scrolling in Subclass of UIScrollView - iphone

Good morning,
I've creates a Subclass of UIScrollView and I now want to know when the user is scrolling in my subclass. For that I implemented it like the following:
ImageScroller.h
#interface UIImageScroller : UIScrollView <UIScrollViewDelegate> {
ImageScroller.m (within the #implementation)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(#"did scroll");
}
The problem is, that the method scrollViewDidScroll doesn't seem to get fired.
Is there any possibility to get it to work?
I also tried to set the delegate to it self, but it doesn't work.
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
self.directionalLockEnabled = YES;
[self setDelegate:self];
}
return self;
}
I added a ScrollView to my XIB-File and set the Class if it to my ImageScroller. Also I've set the Fileowner and I'm using the UIScrollViewDelegate in the .h-File of the ViewController as well as implementing the Method scrollViewDidScroll in the .m-file.
When I set the delegate of my ImageScroller in the code of the .m-file from the XIB like
[imageScroller setDelegate:imageScroller]
the scrollViewDidScroll is fired in my ImageScroller-Subclass, but the one in my ViewController isn't fired, but I need both.
Any solutions for that?
Thanks for your answers in advance.

I ran into the same problem creating a subclass of the UIScrollView but solved it this way. As mentioned above, you can set yourself (subclass instance) as the delegate however this is what I did.
In the sub class I overrode the following methods
//Override this to detect when the user is scrolling, this is also triggered during view
//layout. Here you can check your deceleration rate and determine when the scrolling has
//grinded to a halt.
-(void)setContentOffset:(CGPoint)contentOffset
//Override this to detect when the view has been resized (just a handy override)
-(void)setFrame:(CGRect)frame;
As far as the other UIScrollViewDelegate methods, the View Controller should be responsible for handling those in my opinion.

I think you can try setting self.delegate = self; to receive events.

If anyone is looking for a Swift answer, it's as simple as this:
override var contentOffset: CGPoint {
didSet {
if contentOffset != oldValue {
//same as scrollViewDidScroll
}
}
}

Related

Where to put the UIAlert? [duplicate]

I want to check the pasteboard and show an alert if it contains specific values when the view appears. I can place the code into viewDidLoad to ensure it's only invoked once, but the problem is that the alert view shows too quickly. I know I can set a timer to defer the alert's appearance, but it's not a good work-around I think.
I checked the question iOS 7 - Difference between viewDidLoad and viewDidAppear and found that there is one step for checking whether the view exists. So I wonder if there's any api for doing this?
Update: The "only once" means the lifetime of the view controller instance.
There is a standard, built-in method you can use for this.
Objective-C:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([self isBeingPresented] || [self isMovingToParentViewController]) {
// Perform an action that will only be done once
}
}
Swift 3:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if self.isBeingPresented || self.isMovingToParentViewController {
// Perform an action that will only be done once
}
}
The call to isBeingPresented is true when a view controller is first being shown as a result of being shown modally. isMovingToParentViewController is true when a view controller is first being pushed onto the navigation stack. One of the two will be true the first time the view controller appears.
No need to deal with BOOL ivars or any other trick to track the first call.
rmaddy's answers is really good but it does not solve the problem when the view controller is the root view controller of a navigation controller and all other containers that do not pass these flags to its child view controller.
So such situations i find best to use a flag and consume it later on.
#interface SomeViewController()
{
BOOL isfirstAppeareanceExecutionDone;
}
#end
#implementation SomeViewController
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if(isfirstAppeareanceExecutionDone == NO) {
// Do your stuff
isfirstAppeareanceExecutionDone = YES;
}
}
#end
If I understand your question correctly, you can simply set a BOOL variable to recognize that viewDidAppear has already been called, ex:
- (void)viewDidAppear {
if (!self.viewHasBeenSet) { // <-- BOOL default value equals NO
// Perform whatever code you'd like to perform
// the first time viewDidAppear is called
self.viewHasBeenSet = YES;
}
}
This solution will call viewDidAppear only once throughout the life cycle of the app even if you create the multiple object of the view controller this won't be called after one time. Please refer to the rmaddy's answer above
You can either perform selector in viewDidLoad or you can use dispatch_once_t in you viewDidAppear. If you find a better solution then please do share with me. This is how I do the stuff.
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:#selector(myMethod) withObject:nil afterDelay:2.0];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
static dispatch_once_t once;
dispatch_once(&once, ^{
//your stuff
[self myMethod];
});
}
By reading other comments (and based on #rmaddy 's answer), I know this is not what OP asked for, but for those who come here because of title of the question:
extension UIViewController {
var isPresentingForFirstTime: Bool {
return isBeingPresented() || isMovingToParentViewController()
}
}
UPDATE
You should use this method in viewDidAppear and viewWillAppear. (thanks to #rmaddy)
UPDATE 2
This method only works with modally presented view controllers and pushed view controllers. it's not working with a childViewController. using didMoveToParentViewController would be better with childViewControllers.
You shouldn't have issues in nested view controllers with this check
extension UIViewController {
var isPresentingForFirstTime: Bool {
if let parent = parent {
return parent.isPresentingForFirstTime
}
return isBeingPresented || isMovingFromParent
}
}
Try to set a BOOL value, when the situation happens call it.
#interface AViewController : UIViewController
#property(nonatomic) BOOL doSomeStuff;
#end
#implementation AViewController
- (void) viewWillAppear:(BOOL)animated
{
if(doSomeStuff)
{
[self doSomeStuff];
doSomeStuff = NO;
}
}
in somewhere you init AViewController instance:
AddEventViewController *ad = [AddEventViewController new];
ad.doSomeStuff = YES;
Not sure why you do this in ViewDidAppear? But if you want doSomeStuff is private and soSomeStuff was called only once, here is another solution by notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(doSomeStuff) name:#"do_some_stuff" object:nil];
- (void) doSomeStuff
{}
Then post when somewhere:
[[NSNotificationCenter defaultCenter] postNotificationName:#"do_some_stuff" object:nil];
swift 5
I've tried isBeingPresented() or isMovingToParent.
But It doesn't work.
So I tried below code. and It's work for me!
override func viewDidAppear(_ animated: Bool) {
if (self.isViewLoaded) {
// run only once
}
}
You can use this function in ViewDidLoad method
performSelector:withObject:afterDelay:
it will call that function after delay. so you don't have to use any custom timer object.
and For once you can use
dispatch_once DCD block.Just performSelector in the dispatch_once block it will call performSelector only once when ViewDidLoad is called
Hope it helps

When to use layoutSubview in iOS

I am writing iOS application for iPad that require custom layout.
The layout from portrait and landscape are totally difference, so it can't be solve by using UIAutoResizingMask.
I try to use the layoutSubview Method, but I detected that layout subview is called a lot (from UIScrollView).
How can i reduce the layoutSubview call to optimize the code , or I should call it by my self when ever the device is rotated.
Thank.
For different landscape and portrait design use view controllers methods such as
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
If you create your custom view depending on current orientation, check this orientation by UIDeviceOrientationDidChangeNotification notification and write appropriate code.
in one of the init~ methods:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didChangedOrientation:) name:UIDeviceOrientationDidChangeNotification object:nil];
And action
- (void) didChangedOrientation:(NSNotification *)sender{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (UIDeviceOrientationIsPortrait(orientation)){}}
The fact that layoutSubviews gets called by a child UIScrollView is very unfortunate, but there's an (ugly) workaround:
#interface MyClass : UIView {
BOOL reallyNeedsLayout_;
}
#end
#implementation MyClass
- (void)setNeedsLayout
{
[super setNeedLayout];
reallyNeedsLayout_ = YES;
}
- (void)setFrame:(CGRect)rect
{
[super setFrame:rect];
reallyNeedsLayout_ = YES;
}
- (void)layoutSubviews
{
if (!reallyNeedsLayout_) return;
reallyNeedsLayout_ = NO;
// Do layouting.
}
#end
Not the best solution but seems to work reasonably well.
You should not do expensive calculations in layoutSubviews:
Speaking from experience I would personally only adjust your layout based upon deviceDidRotateSelector notifications.
I have an updatePortrait method and an updateLandscape method and call whichever is necessary.

iPhone SDK: detect view that was tapped

In my application I have a lot of little UIImageViews and a view off to the side. What I need is that view off to the side to be able to detect which image view was tapped and give me all the information about it (much like with (id)sender) as well as its coordinates. What is the best way to do this?
This is the internal class from my project. I think you get the idea.
As an option you can create protocol for the owner (or delegate).
You can obtain coords using
-[UITouch locationInView: someView]
Here is the code:
#interface _FaceSelectView : UIImageView {
#protected
VIFaceSelectVC* _owner;
}
-(id) initWithOwner:(FaceSelectVC*) owner;
#end
#implementation _FaceSelectView
-(id) initWithOwner:(FaceSelectVC*) owner {
if( self = [super init] ) {
_owner = owner;
self.userInteractionEnabled = YES;
}
return self;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[_owner _touchesBeganIn: self withTouch: (UITouch*)[touches anyObject]];
}
-(void) touchesEnded:(NSSet*) touches withEvent:(UIEvent*) event {
[_owner _touchesEndedIn: self withTouch: (UITouch*)[touches anyObject]];
}
-(void) touchesMoved:(NSSet*) touches withEvent:(UIEvent*) event {
[_owner _touchesMovedIn: self withTouch: (UITouch*)[touches anyObject]];
}
#end
I would subclass UIImageView and add a new property for the target view you would like to message. Then reenable userInteractionEnabled and add a action to touchesUpInside.
In the action method of the custom subclass call a method on the target view in which you also give the object of the custom subview. Basically you use delegation and pass in the delegate call all parameters you need.
Add tag to UIImageView like myUIImageView.tag = intNumber;
and in side touchesBegan or you can call any common method for all your UIImageView and use tag to identify which view is tapped.

UIView did appear?

I'm wondering, is there a way to get a delegate or something, when a particular UIView has been shown on the screen ?
Swift version. Inside your UIView class just:
override func willMove(toWindow newWindow: UIWindow?) {
super.willMove(toWindow: newWindow)
if newWindow == nil {
// UIView disappear
} else {
// UIView appear
}
}
Try these:
– didAddSubview:
– willRemoveSubview:
– willMoveToSuperview:
– didMoveToSuperview
– willMoveToWindow:
– didMoveToWindow
- viewDidAppear:
If you manage your logic directly inside the UIView, use:
- didMoveToSuperview
If you manage your logic inside a UIViewController, use :
- viewDidAppear:(BOOL)animated
If you are managing the UIView via a UIViewController, then you can use the -viewDidAppear: method:
- (void) viewDidAppear:(BOOL) animated {
//do stuff...
[super viewDidAppear:animated];
}
Another way to find out when a control is on screen is to subclass the View or Control and override drawRect...
However, it's called when it's drawn and not only when first shown. So it's only sometimes what you want. It worked for my case. Make sure to call super as well! =)

frame size does not change in landscape mode

I have a UINavigationController that can rotate to landscape mode and that it initializes a new UIViewController and pushes it on the stack. On the UIViewController's viewDidLoad I just print the self.view.frame.size.
If the UINavigationController is in Portrait mode, the View Controller will print {320, 460}. However, when the Navigation Controller is in Landscape, the frame size is exactly the same. (To clarify, in the second case the UIViewController is initialized while the phone is already in landscape mode.)
Shouldn't the frame size of the view be rotated? And if not how I can I find the correct frame size without hardcoding the numbers?
You can't rely on the frame in landscape mode; you have to use a combination of bounds and center; frame is generated using a combination of those, and when there's a non-identity transform applied (as there is in landscape), it gets a little weird.
first you need to set your view to resize automatically with a proper autoresizingMask.
with this your view will adapt to the size of the controller itself.
you can check this yourself with an NSLog. But don't put it in loadView, this is too early. Put this in viewWillAppear.
If you set the autoresizingMask of your view with Interface Builder you should turn off the Simulated Interface Elements in the Attributes inspector. If any of these is on you can't change the autoresizingMask in the Size inspector.
This is a pain and it's still true in iOS 4.2 for iPads. The way I solved this is to subclass the UIView associated with the UIViewController. I did this in interface builder but I suppose one could also do this in code somehow. In interface builder select the UIView in the UIViewController then hit the (i) icon in the upper right of the Inspector window. Under class identity hit the popup and chose the UIView subclass below.
The approach is that this UIView subclass overrides the layoutSubviews method, finds the next UIViewController, determines if it implements a reactToLayout method (which is a method that you have to implement in the UIViewController subclass for the view controller of this view). If the reactToLayout method exists in the first UIViewController found, it is invoked.
The reactToLayout method in the view controller then does whatever one needs to do which it will be able to do successfully since the view's frame is set up properly by this time (unlike in ViewDidLoad, viewWillAppear, or even viewDidAppear). I have a method that I call anytime the orientation or frame changes. It's a pain but I store the last frame laid out and last orientation laid out in internal variables of the view controller. The internal layout for new orientation or frame change method compares these to the view's current frame and requested or current orientation so that it doesn't unnecessarily layout stuff over and over.
Here's the code:
UILayoutSubviewsView.h
#import <UIKit/UIKit.h>
#interface UILayoutSubviewsView : UIView {
}
#end
UILayoutSubviewsView.m
#import "UILayoutSubviewsView.h"
// Create this to avoid a warning that this method does not exist for UIViewControllers
// this is OK since we check to see that it does exist before invoking it
#interface UIViewController(UndocumentedMethodForUIViewController)
-(void) reactToLayout;
#end
#implementation UILayoutSubviewsView
// Pass this up to our view controller if it supports the reactToLayout method
// (this is the whole reason for the class)
-(void) layoutSubviews {
[super layoutSubviews];
// Look for the first next responder that is a UIViewController
UIViewController *ourViewController = nil;
id myNextResponder = [self nextResponder];
while (myNextResponder != nil && ourViewController == nil) {
if ([myNextResponder isKindOfClass:[UIViewController class]]) {
ourViewController = myNextResponder;
}
else {
myNextResponder = [myNextResponder nextResponder];
}
}
// If we got a view controller, then see if it supports the reactToLayout method
if (ourViewController != nil) {
if ([ourViewController respondsToSelector:#selector(reactToLayout)]) {
// Invoke the view controller's reactToLayout method
[ourViewController reactToLayout];
}
}
}
#end
YourViewController.h
#import <UIKit/UIKit.h>
#interface YourViewController : UIViewController {
CGRect lastLayedOutFrame;
UIInterfaceOrientation lastLayedOutOrientation;
}
#pragma mark -
#pragma mark Instance Methods
-(id) init;
-(void) reactToLayout;
#end
YourViewController.m
#import "YourViewController.m"
#pragma mark Private Interface Category
#interface YourViewController()
-(void) setViewForCurrentFrameAndRequestedOrientation:(UIInterfaceOrientation) interfaceOrientation;
#end
#implementation YourPadViewController
-(id) init {
// First our super then set ourselves up
if (self = [super initWithNibName:#"YourViewController" bundle:nil]) {
// Initialize some basic stuff
lastLayedOutFrame = CGRectZero;
lastLayedOutOrientation = UIDeviceOrientationUnknown;
}
return self;
}
-(void) viewWillAppear:(BOOL) animated {
[super viewWillAppear:animated];
// Make sure we're showing the right stuff in the right place
[self setViewForCurrentFrameAndRequestedOrientation:UIDeviceOrientationUnknown];
}
-(void) viewDidAppear:(BOOL) animated {
[super viewDidAppear:animated];
// Make sure we're showing the right stuff in the right place
[self setViewForCurrentFrameAndRequestedOrientation:UIDeviceOrientationUnknown];
}
-(void) reactToLayout {
// Make sure we're showing the right stuff in the right place
[self setViewForCurrentFrameAndRequestedOrientation:UIDeviceOrientationUnknown];
}
#pragma mark -
#pragma mark Rotation Support
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
return YES;
}
// This is called right before the actual rotation
-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation duration:(NSTimeInterval) duration {
[super willAnimateRotationToInterfaceOrientation:interfaceOrientation duration:duration];
// Make sure we're showing the right stuff in the right place
[self setViewForCurrentFrameAndRequestedOrientation:interfaceOrientation];
}
// Make the necessary adjustments for the different view orientations
-(void) setViewForCurrentFrameAndRequestedOrientation:(UIInterfaceOrientation) interfaceOrientation {
// Set up the requested orientation (need this to handle the Unknown case)
UIInterfaceOrientation requestedOrientation;
if (interfaceOrientation != UIDeviceOrientationUnknown) {
requestedOrientation = interfaceOrientation;
}
else {
requestedOrientation = [[UIDevice currentDevice] orientation];
}
// See if we have anything to do
if (!(CGRectEqualToRect(self.view.frame, lastLayedOutFrame) && lastLayedOutOrientation == requestedOrientation)) {
// Do whatever needs to be done
// Record our last layed out frame and orientation
lastLayedOutFrame = self.view.frame;
lastLayedOutOrientation = requestedOrientation;
}
}
Hi every one
I think there is a simple solution that do the job for me
you can use currentSize instead of self.view.frame.size
in yourClass.h
#interface yourClass : UIViewController {
CGSize currentSize;
}
#property (nonatomic, readwrite)CGSize currentSize;
#end
in yourClass.m
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation != self.interfaceOrientation) {
CGSize newSize;
// 20 is the status bar height
newSize.width = self.view.bounds.size.height + 20;
newSize.height = self.view.bounds.size.width - 20;
currentSize = newSize;
//any other necessary code
}
}