HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Changing image of a UIImageView based on a scrollView contentOffset

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
imagecontentoffsetbasedscrollviewchanginguiimageview

Problem

My task is pretty simple and my code works, it's just that my code feels a bit clumsy to me (and probably will for you too).

I have a view controller (HomeViewController) with a scrollView, inside this scroll view I have a label, and the scroll view is set to scroll horizontally. So now we have a view controller with a label and you can scroll this label.

What I want to do here is, whenever I scroll down the label a V sign icon will come from top, and whenever I the scroll view property contentOffset.y <= -73 I want to change the V sign icon to a different V sign icon.

I have some more parameters for changing this label. For instance, if there is even a label, because I take this label from a table view of another view controller and I can delete objects from this table view, and if there are no objects in this table view there is no label, in this case I don't want to present those icons when scrolling (this is also something I did in a pretty clumsy way, I think the best will be to disable the scroll view in this case).

This is my code (the relevant code, I'm working with nib's just so you know):

HomeViewController.m:

```
#import "HomeViewController.h"
//there are some more classes I'm importing here

@interface HomeViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *currentScrollImage; //size 50X50
@property (weak, nonatomic) IBOutlet UIImageView *vMarkIcn; //size 28X20
@property (weak, nonatomic) IBOutlet UIImageView *greenVmarkIcn; //size 50X50
@property (assign, nonatomic) BOOL labelRemoving;

@end

@implementation HomeViewController {

int fingerLiftedAtOffset;
}

//stackTableViewController is the table view where im taking the strings to populate the label

  • (id)init {



self = [super initWithNibName:@"HomeViewController" bundle:nil];
if (self) {
// Do something
stackTableViewController = [[StackTableViewController alloc] init];
stackTableViewController.delegate = self;
}
return sel

Solution

None of these points really help with what you're primarily asking about, and by all means, you certainly shouldn't mark this answer as accepted as it doesn't address your primary concern, but there are a few things I do want to point out about your code.

-
Unless you're trying to report an error or warning to an end user (read: someone who has downloaded this from the app store), you should wrap all of your NSLog() statements in #if DEBUG statements so that they're only printed in debug mode and not printed in your final release build:

#if DEBUG
    NSLog(@"this is a debug log");
#endif


-
-73 is a magic number. This has no meaning to anyone, and it may be that you'll forget its meaning at some point as well. More importantly however, what if you decide that you want to use a different value? In how many locations would you have to be certain you changed the value? Instead, we should define a constant and use that:

static CGFloat const kSomeDescriptiveVariableName = -73.0f // offset to change image


  • Our class names should have some sort of prefix. There's no namespaces in Objective-C. If you use StackTableViewController, and then later implement a library that also has a StackTableViewController, you're going to have problems. Now, fortunately, any library that's worth implementing probably won't have classes without prefixes... but that really doesn't mean you get to get away with not using prefixes. Xcode will auto generate and prepend these prefixes for you anyway if you just give it a value to use.



-
Finally, our abundance of literal strings to load our nibs is actually the same as the -73 magic number problem. It's far less likely that you'll completely change the filename of the nib file, but it's not completely unheard of. For my personal preference, I prefer having a single file with all of my nib's defined as constants:

// NibNames.h

#ifndef ProjectName_NibNames_h
#define ProjectName_NibNames_h

static NSString * const kNIB_HomeViewController = @"HomeViewController";
static NSString * const kNIB_StackTableViewController = @"StackTableViewController";

#endif


This gives us the same advantage as defining -73 as a constant, we can use it multiple places, and if we ever need to change it, it's one change in one spot and then we're done. But there's an added bonus when dealing with string literal. It's very easy to misspell a string literal. For me personally, I find misspellings and typos more frequent when I'm coding because code-completion is so helpful it makes me lazy. When using string literals, we get no code completion. When we define constants like this, we get code completion. Just start typing kNIB_ and you can use the arrow keys or the mouse scroll wheel to cycle through all your defined nib file names you've defined.

Code Snippets

#if DEBUG
    NSLog(@"this is a debug log");
#endif
static CGFloat const kSomeDescriptiveVariableName = -73.0f // offset to change image
// NibNames.h

#ifndef ProjectName_NibNames_h
#define ProjectName_NibNames_h

static NSString * const kNIB_HomeViewController = @"HomeViewController";
static NSString * const kNIB_StackTableViewController = @"StackTableViewController";

#endif

Context

StackExchange Code Review Q#78164, answer score: 6

Revisions (0)

No revisions yet.