Setting alpha on custom range slider (NMRangerSlider) - ios

I'm using muZZkat's custom range slider: NMRangeSlider#github1
But i need to fade the slider. When i create the range slider, i put it into a content view. I've tried setting the alpha of the content view to .7
UIView *sliderView = [self configureLabelSliderWithSize:CGSizeMake(300, 70)];
sliderView.alpha = .4;
but it does not give the desired effect:
The UIImageView upperHandle and lowerHandle also fade out which shows the background track (the blue part). I'd rather have the handle's alpha be higher so you can't see the blue part behind it.
I tried adding a method to the ranger slider class:
-(void)toggleAlpha {
self.trackBackground.alpha = .3;
self.track.alpha = .3;
self.lowerHandle.alpha = .3;
self.upperHandle.alpha = .3;
}
but this does not change the alpha at all.
The UIImageViews are all initalized in the -(void)addSubviews method:
- (void) addSubviews
{
//------------------------------
// Track Brackground
self.trackBackground = [[UIImageView alloc] initWithImage:self.trackBackgroundImage];
self.trackBackground.frame = [self trackBackgroundRect];
//------------------------------
// Track
self.track = [[UIImageView alloc] initWithImage:self.trackImage];
self.track.frame = [self trackRect];
//------------------------------
// Lower Handle Handle
self.lowerHandle = [[UIImageView alloc] initWithImage:self.lowerHandleImageNormal highlightedImage:self.lowerHandleImageHighlighted];
self.lowerHandle.frame = [self thumbRectForValue:_lowerValue image:self.lowerHandleImageNormal];
//------------------------------
// Upper Handle Handle
self.upperHandle = [[UIImageView alloc] initWithImage:self.upperHandleImageNormal highlightedImage:self.upperHandleImageHighlighted];
self.upperHandle.frame = [self thumbRectForValue:_upperValue image:self.upperHandleImageNormal];
[self addSubview:self.trackBackground];
[self addSubview:self.track];
[self addSubview:self.lowerHandle];
[self addSubview:self.upperHandle];
}
and if i set their alpha lower here, it works.
I don't understand why i can't alter the alpha from anywhere else?

I just added the NMRangeSlider-Demo and checked it out.
I think the problem is that you're probably calling toggleAlpha in viewDidLoad method, right?
You should instead call in the viewDidAppear method, since layoutSubviews method (which in turn calls addSubview) in the NMRangeSlider class, is called after viewDidLoad and before viewDidAppear, so whatever you do in viewDidLoad will be reset as the subviews are initialized in addSubview method.
Just adding toggleAlpha in viewDidAppear worked for me.

Related

UIWebView black color at bottom increases with Animation?

There are some posts, where UIWebView bottom is black and can be resolved by two simple ways.
1. Clear Background of UIWebView
2. Set Opaque to NO.
However, this only solves problem for static UIWebView which is not changing its Frame or Constraints.
In my case, I have a UIWebView, It is very simple Drag and Drop on Storyboard's ViewController's View. No Inheritance. I set Background clear and set opaque to NO.
It looks fine up to now. But when I apply animation, using Facebook POP library
1. SetOpaque = YES, With Animation is clips the UIWebView's content showing black color.
2. SetOpaque = NO, With Animation is clips the UIWebView's content showing nothing (a View or something comes in front)
-(void) setConstraint {
_topConstraintValue = self.topConstraint.constant - 200;
NSLog(#"Value is UP %f " ,_topConstraintValue);
}
- (IBAction)animateUp:(UIButton *)sender {
[self setConstraint];
[VSAnimation popChangeConstraintForBasicAnimation:self.topConstraint begin:0.5 newConstant:_topConstraintValue withDuartion:0.5 isEaseInAnimation:NO withCompletionBlock:nil];
}
-(void) setConstraintDown {
_topConstraintValue = 0;
_topConstraintValue = self.topConstraint.constant;
NSLog(#"Value is Down %f " ,_topConstraintValue);
}
- (IBAction)animateDown:(UIButton *)sender {
[self setConstraintDown];
[VSAnimation popChangeConstraintForBasicAnimation:self.topConstraint begin:0.5 newConstant:[self getAnimatableConstraint:_topConstraintValue] withDuartion:0.5 isEaseInAnimation:NO withCompletionBlock:nil];
}
-(float)getAnimatableConstraint:(float)constant{
return constant+200.0;
}
-(float)getReverseAnimatableConstraint:(float)constant{
return constant-200.0;
}
Where, method inside VS is as below
//VSAnimation.m
+(void)popChangeConstraintForBasicAnimation:(NSLayoutConstraint *) layoutConstraintToPopChange begin:(float) beginTime newConstant:(CGFloat) newConstant withDuartion:(float)duration isEaseInAnimation:(BOOL)isEaseIn withCompletionBlock:(void (^)(POPAnimation *anim, BOOL finished)) completitionBlock{
NSString *key = [NSString stringWithFormat:#"constraints_changed_%#", layoutConstraintToPopChange];
POPBasicAnimation *basicAnim = [layoutConstraintToPopChange pop_animationForKey:key];
if(basicAnim){
basicAnim.toValue = #(newConstant);
basicAnim.completionBlock = completitionBlock;
}else{
basicAnim = [POPBasicAnimation animationWithPropertyNamed:kPOPLayoutConstraintConstant];
if(isEaseIn)
basicAnim.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
else
basicAnim.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
basicAnim.toValue = #(newConstant);
basicAnim.completionBlock = completitionBlock;
basicAnim.beginTime = [self timeWithDelay:beginTime];
basicAnim.delegate = self;
basicAnim.duration = duration;
[layoutConstraintToPopChange pop_addAnimation:basicAnim forKey:key];
}
}
What is this black line/transparent line at bottom.
How to stop this from increasing when one animates UIWebView.
IMAGES:
SHOWING ANIMATION UP & ANIMATION DOWN.
#VS, After reading you source code, I saw the constraint of the 2nd webview seems to be set wrongly. After I removed the height constraint WV2.height = 0.095 * height, the problem disappeared. You have to set the WV2's height constraint properly.

create a UILabel when view appears and destroy it when view disappears

For the tutorial for my app, I am trying to create a UILabel that drifts across the displayed screen when a view appears and then is destroyed so that if the user comes back to that view during the tutorial, the UILabel will be created anew and again drift across the page. Here's a sample of one of the custom view controllers I am displaying with my UIPageViewController:
//this is just a custom UILabel with some padding
#property (nonatomic) PaddedLabel *directionsLabel;
//I have tried setting UILabel to nil or removing it from view
-(void)viewWillDisappear:(BOOL)animated
{
NSLog(#"view is disappearing");
//this does not remove the label
self.directionsLabel = nil;
//nor does this
[self.directionsLabel removeFromSuperview];
}
- (void)viewDidAppear:(BOOL)animated
{
[self messageTutorial];
}
- (void)messageTutorial
{
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
PaddedLabel *directionsLabel = [[PaddedLabel alloc] initWithFrame:CGRectMake(_width/2, _height/2, 100, 100)];
directionsLabel.text = #"test";
CGRect f = directionsLabel.frame;
f.size = size;
directionsLabel.frame = f;
directionsLabel.center = self.view.center;
f = directionsLabel.frame;
f = directionsLabel.frame;
f.origin.y = .1*height;
directionsLabel.frame = f;
[self.view addSubview:directionsLabel];
[UIView animateWithDuration:TUTORIAL_DISAPPEAR_TIME animations:^{
directionsLabel.alpha = .5;
CGRect f = directionsLabel.frame;
f.origin.y = height - f.size.height*1.4;
directionsLabel.frame = f;
NSLog(#"animating");
} completion:^(BOOL finished) {
[directionsLabel removeFromSuperview];
//this also doesn't actually remove the label
}];
}
The problem is that if the user pages back to see this view she now sees a new label and the old one, so that if you page back and forth back and forth you end up with many many labels all saying the same thing, in different stages of progressing across the screen.
How can I remove the UILabel when the view disappears and add/create a new one when the view appears/reappears?
Thank you.
The code in your viewWillDisappear method is backwards. You need:
- (void)viewWillDisappear:(BOOL)animated {
NSLog(#"view is disappearing");
[self.directionsLabel removeFromSuperview];
self.directionsLabel = nil;
}
As you had it, setting self.directionsLabel to nil before trying to remove it results in a no-op.
Also, be sure to set self.directionsLabel when you create the label.
Instead of setting your label to nil and effectively destroying the label object (assuming automatic reference counting is on) rather use the following method to hide and show the label as and when your need it.
[self.directionsLabel setHidden:YES]; // hides it
[self.directionsLabel setHidden:NO]; // shows it
You've got the right idea setting objects you're not using to nil and removing them from the super view but it's over kill. A UILabel object uses a negligible amount of memory and you're better off creating the object once and then changing it's properties as you need to.
You don't seem to be setting self.directionsLabel to anything when you create the directionsLabel inside the messageTutorial method. It is a local instance of the label there. You should set it in the method somewhere.
Afterwards, removing it from the superview in viewWillDisappear will work (tested to verify).

Can't hide UIImageView via setHidden:

I have a custom UITableViewCell in which i create an Icon like this:
self.icon = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 50, 50)];
[self.icon setImage:[AppearanceProxyController getImageForAssetIdentifier:#"import"]];
[self addSubview:self.icon];
which works just fine. In addition there is my method to hide the icon:
-(void)hideIcon{
dispatch_async(dispatch_get_main_queue(), ^{
[self.icon setHidden:true];
});
}
which ... has no effect at all and i have no clue why.
the method is called when the cell is tapped from its tableviewcontroller.
the check states it as hidden but it is not.
UIImageView: 0x17d37d20; frame = (15 15; 50 50); hidden = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x17f8f4c0>> is hidden: 1
setting the uiimage to nil od the alpha to 0.0 has no effect either. What am i doing wrong?
You probably have more than one UIImageView instance, but only have a reference to the latest one. When you hide that, you'll see the old instance underneath which (if they show the same image) looks like hiding didn't work.
So you need to make sure that you call removeFromSuperview on the old instances or need to make sure that you don't create more than one.
Your problem is you are creating self.icon in every uitableview cell.
You alloc init the icon every time so self.icon hold the last object which you alloc init. if you hide the self.icon then it hides the last object.
So If you want to hide some image view just pass this object in your hide icon method like this
-(void)hideIcon : (UIImageView *)imageView{
dispatch_async(dispatch_get_main_queue(), ^{
[imageView setHidden:true];
});
}
And call it when you want to hide

Is UICollectionView.backgroundView broken

I'm writing something relatively simple, or so I thought.
Firstly, the code, for which I'm trying to place an image on the background of the UICollectionView if there are no results returned from my server. The image is 200 by 200:
UIView *myView = [[UIView alloc] initWithFrame:self.view.bounds];
CGRect myViewSpace = self.view.bounds;
CGFloat myX = (myViewSpace.size.width /2.0) - 100;
CGFloat myY = (myViewSpace.size.height /2.0) - 100;
UIImageView *imView = [[UIImageView alloc] initWithFrame:CGRectMake(myX, myY, 200, 200)];
imView.image = [UIImage imageNamed:#"imNotHome"];
[myView addSubview:imView];
myCollectionView.backgroundView = myView;
Once there are results, I want to be able to remove it.
I thought it'd be as simple as placing the following, before I reloaded the UICollectionView:
[myCollectionView.backgroundView removeFromSuperview];
However, it appears to be doing nothing.
Am I missing something?
Thanks in advance!
It should be done this way instead:
myCollectionView.backgroundView = nil;
Explanation: You should unset the UICollectionView's background in the same way as you set it. You didn't set it by manipulating the view hierarchy, but by setting the background property. You did call addSubview in the previous line, but that was to add a subview to your background view, not to add the background view itself.
Edit:
There is a very good article about this UICollectionView bug here:
http://blog.spacemanlabs.com/2013/11/uicollectionviews-backgroundview-property-is-horribly-broken/
The solution the author gives is to reset the background to an empty opaque view:
UIView *blankView = [UIView new];
blankView.backgroundColor = [UIColor whiteColor];
[myCollectionView.backgroundView removeFromSuperview];
myCollectionView.backgroundView = blankView;
Also, the author recommends not using the backgroundView property at all but doing it yourself:
Frankly, I think the best solution is to just ignore the backgroundView property all together. Instead, make the collection view’s background clear, and implement your own backgroundView; just throw a view behind the collection view.

Shadow in separate view from UIImage for dynamic adjustments

I would like to obtain this effect (http://stackoverflow.com/questions/7023271/how-to-adjust-drop-shadow-dynamically-during-an-uiimageview-rotation) but from a more complex image than just a red square ! If the link ever gets broken, it's a about how to adjust drop shadow dynamically during an UIImageView rotation.
So I tried implementing something but I just can't get the shadow in a separate layer... Here is my code, very simple, but doesn't work:
// here is my code
- (void)viewDidLoad {
[super viewDidLoad];
testView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"handNoShadow.png"]];
testViewShadow = [[UIView alloc] initWithFrame:testView.frame];
testViewShadow.layer.shadowPath = [[testView layer] shadowPath];
testViewShadow.layer.shadowOpacity = 1.0;
testViewShadow.layer.shadowOffset = CGSizeMake(10, 10);
testViewShadow.layer.shadowRadius = 5.0;
[self.view addSubview:testViewShadow];
[self.view addSubview:testView];
}
PS: i did #import
I do get an image but no shadow... =(
Any lead, help, code, link... is welcome !
Thanks
possible cause:
your testViewShadow.clipToBounds property is set to YES (should be NO)
your testViewShadow do the drawing of the shadow correctly but another UIView is on top and mask it. Check your Z order. Either the order in Storyboard/Nib file (or the order you added the subviews programmatically). Last in the list (or last one added) is on top. For my app I had to put the UIView that need a shadow last so that no other view mask it.

Resources