UIView setFrame problem - ipad

I must do something wrong, but dont know what..
I try to add a subView with this code:
subMenuView = [[UISubMenuViewMainController alloc] init];
[subMenuView.view setFrame:CGRectMake(10,0,990,100)];
subMenuView.view.backgroundColor = [UIColor whiteColor];
[self.view addSubview:subMenuView.view];
I want my view to be at (10,0) and have 990/100 in width/height
but i dont get the expected result
Let me if I m wrong, If I want a 10x10 square view at the center i have to add the following line:
[subMenuView.view setFrame:CGRectMake(512,384,10,10)];
That s not what I get, the position is correct, but the width/height are wrong, any ideas?

if you use autolayout,the call setFrame have no use,try call setTranslatesAutoresizingMaskIntoConstraints before setFrame

problem fixed by setting
self.view.autoresizesSubviews = NO;

The code is a little unconventional, but I'll hazard a guess it's because you are setting the view's frame before you add it as a subview. Hence the subview's position probably gets changed by layoutSubviews before you see it.
So try putting the second line of code last and see if that does the trick.

Try using floats instead of ints:
[subMenuView.view setFrame:CGRectMake(10,0,990,100)];
to:
[subMenuView.view setFrame:CGRectMake(10.0f,0.0f,990.0f,100.0f)];

Related

How to get a circle image in UIImageView

I tried to get a circle image using layer, here is my code
_compassArrow.layer.cornerRadius = _compassArrow.frame.size.width / 2;
_compassArrow.layer.masksToBounds = YES;
_compassArrow.layer.borderColor = [UIColor whiteColor].CGColor
the compassArrow is an imageview which display the compass image. And when I run my program, it looks terrible:
my actual picture
I don't know what happened to it. I've add some constraints to it, to make it has equal width with the device. Does this influence my image?
I think you set cornerRadius before your constraints are applied. Try to put this code in layoutSubviews or viewDidLayoutSubviews for example.
This way, the _compassArrow.frame.size.width value will be the one after constraints applied on it, and you'll get the correct cornerRadius.
Here is a piece of code that should allow you to do this.
_compassArrow.layer.borderWidth = 1.0
_compassArrow.layer.masksToBounds = false
_compassArrow.layer.borderColor = UIColor.whiteColor().CGColor
_compassArrow.layer.cornerRadius = profilePicture.frame.size.width/2
_compassArrow.clipsToBounds = true

Key-Frame Animation appears fuzzy when moving Frames?

I use JazzHands to create a key frame based animation in a UIScrollView.
Here is an example. Look at the view at the top. When you move from page to page. While the animation is running the view at the top is slightly moving from left to right. The animation appears a bit fuzzy.
Here is the code taken from the example here:
IFTTTFrameAnimation *titleView1FrameAnimation = [IFTTTFrameAnimation new];
titleView1FrameAnimation.view = self.titleView1;
[self.animator addAnimation:titleView1FrameAnimation];
[titleView1FrameAnimation addKeyFrame:[[IFTTTAnimationKeyFrame alloc] initWithTime:timeForPage(1)
andFrame:self.titleView1.frame]];
[titleView1FrameAnimation addKeyFrame:[[IFTTTAnimationKeyFrame alloc] initWithTime:timeForPage(2)
andFrame:CGRectOffset(self.titleView1.frame, timeForPage(2), 0)]];
[titleView1FrameAnimation addKeyFrame:[[IFTTTAnimationKeyFrame alloc] initWithTime:timeForPage(3)
andFrame:CGRectOffset(self.titleView1.frame, timeForPage(3), 0)]];
[titleView1FrameAnimation addKeyFrame:[[IFTTTAnimationKeyFrame alloc] initWithTime:timeForPage(4)
andFrame:CGRectOffset(self.titleView1.frame, timeForPage(4), 0)]];
When running the demo take a look at the part marked with red in the following screenshot:
Edit: Here is the code containing this problem: https://github.com/steffimueller/Intro-Guide-View-for-Talk.ai
How can I make the animation running smooth and less fuzzy?
This is due to the frame rate in the JazzHands IFTTTAnimatedScrollViewController being set for non-retina displays. You need to double the number in timeForPage, and also use double the number of the contentOffset in animate, but use the original non-doubled values of timeForPage in places where you were using that for laying out the positions of views instead of using it for the animation time.
Here's a Gist of the changes you'd have to make to your example to get it working. Fixed Demo Gist
You need this method for setting the animation times:
#define timeForPage(page) (NSInteger)(self.view.frame.size.width * (page - 1) * 2.0)
And this one for setting the centers of your views based on the page dimensions:
#define centerForPage(page) (NSInteger)(self.view.frame.size.width * (page - 1))
Then you need to override the scrollViewDidScroll: method in IFTTTAnimatedScrollViewController to use double the numbers it's currently using.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[self.animator animate:(NSInteger)(scrollView.contentOffset.x * 2)];
}
We'll work on getting the JazzHands demo updated!
Before you start your animation call:
view.layer.shouldRasterize = YES; (seems that you are using objective-c so I put YES here, for swift should be true)
As soon as the animation is finished call
view.layer.shouldRasterize = NO; (seems that you are using objective-c so I put NO here, for swift should be false)
You should always use it when you animating a view
You can see more details about it in the WWDC 2012 Polishing Your Interface Rotations video (paid developer subscription needed)
I hope that helps you!
[EDIT]
Every Time you call the method animate set shouldRasterize to YES before it, like in the example bellow:
titleView1FrameAnimation.view.layer.shouldRasterize = YES;
[self. titleView1FrameAnimation animate:scrollView.contentOffset.x];
I've messed around a bit with the code and it seems that keeping those top views in place while the scrollview is scrolling made it jiggle left and right.
What I did is take out the title view from the scroll view and add it to the view controller view.
You can see it in action here
Later edit:
To actually see what I've changed you can check the file differences in Git. Basically I moved your titleViews (titleView1, titleView2, etc) from the scrollView to the view controller's view (so basically I've replaced all the lines that were like this:
[self.scrollView addSubView:self.titleView1]
to something like this:
[self.view addSubView:self.titleView1]
After that I've also took out the keyframe animations that were keeping your title views in place since they were not moving with the scrollview anymore. Practically I've deleted all the lines that were adding a frame animation to your titleviews from each configurePageXAnimation.
2nd question answer:
Say your screenshot view is called screenshotView. You can go ahead and create it like this:
UIImageView *screenshotView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ScreenshotImage"]];
[self.view addSubview:screenshotView];
self.screenshotView = screenshotView;
[self.screenshotView setCenter:CGPointMake(self.view.center.x + self.view.bounds.size.width, <#yourDesiredY#>)];
And animate it like this:
//screenshotView frame animation
IFTTTFrameAnimation *screenshotViewFrameAnimation = [IFTTTFrameAnimation new];
screenshotViewFrameAnimation.view = self.screenshotView;
[self.animator screenshotViewFrameAnimation];
[screenshotViewFrameAnimation addKeyFrame:[[IFTTTAnimationKeyFrame alloc] initWithTime:timeForPage(1) andFrame:self.screenshotView.frame]];
[screenshotViewFrameAnimation addKeyFrame:[[IFTTTAnimationKeyFrame alloc] initWithTime:timeForPage(2) andFrame:CGRectOffset(self.screenshotView.frame, -self.scrollView.bounds.size.width, 0.0)]];
[screenshotViewFrameAnimation addKeyFrame:[[IFTTTAnimationKeyFrame alloc] initWithTime:timeForPage(3) andFrame:CGRectOffset(self.screenshotView.frame, -self.scrollView.bounds.size.width, 0.0)]];
[screenshotViewFrameAnimation addKeyFrame:[[IFTTTAnimationKeyFrame alloc] initWithTime:timeForPage(4) andFrame:CGRectOffset(self.screenshotView.frame, -self.scrollView.bounds.size.width * 2, 0.0)]];

UIButton's text is not centered

Hi I have an simple application where I am supposed to change the frame of a UIButton based on its super view and based on its frame the title size should get change.
I am using Autolayout to change the frame of that UIButton. But not getting any idea how to change the font size of that UIButton.
Here is the code I have tried so far.
1. [self.cButton sizeToFit] ;
2. [self.cButton.titleLabel sizeToFit] ;
3. self.cButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter ;
4. self.cButton.titleLabel.adjustsFontSizeToFitWidth = YES;
5. self.cButton.titleLabel.textAlignment = NSTextAlignmentCenter ;
6. self.cButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
Like I have tried many things.
Can anyone tell me if I am missing something here and I am doing something wrong.
I was having this issue and it turned out that I was overriding layoutSubviews and had forgotten to call [super layoutSubviews].

UIInterpolatingMotionEffect works randomly

I'm adding a UIInterpolatingMotionEffect to some views in UITableViewCells this way:
UIInterpolatingMotionEffect *horizontalEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:#"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
UIInterpolatingMotionEffect *verticalEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:#"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
horizontalEffect.minimumRelativeValue = #(-horizontal);
horizontalEffect.maximumRelativeValue = #(horizontal);
verticalEffect.minimumRelativeValue = #(-vertical);
verticalEffect.maximumRelativeValue = #(vertical);
UIMotionEffectGroup *effectsGroup = [UIMotionEffectGroup new];
effectsGroup.motionEffects = #[horizontalEffect, verticalEffect];
[view addMotionEffect:effectsGroup];
The problem is that the effect only appears randomly, where some views get the effect and some don't. After pushing a view controller and going back, some others work and some others don't.
Is there something I'm missing? Should the effect be applied every time the cell is reused?
I had the same problem. I fixed it by forcing a redraw of all the cells - using reloadSections:withRowAnimation: will likely work for most people (or a similar method), although for me I ultimately had to write my own cell inits and reuse code that let me keep a reference to every created cell in a mutable array, and then clear that array and build from scratch when I chose to. Hope that helps.
UPDATE:
Had to completely remove my previous answer, because I have finally found a better solution.
It looks like iOS7/8 messes with the motion effects of the views inside the table/collection views once the cell is redrawn / dequeued. You need to make sure your motion effect are set up / updated after the cell is dequeued / set up.
To properly do so, you need to move your motion effect logic to the -layoutSubviews method.
Then just send the [self setNeedsLayout] message within your constructor and methods, which you use to update the cell contents after the cell is dequeued and updated.
This solved the issue completely for me.
Using a UICollectionView, I was experiencing the same problem. After pushing a new controller, then going back to the UICollectionView, some of my cells' UIInterpolatingMotionEffects stopped functioning, but were still listed on the view's motionEffects property.
Solution:
I called to setup my motion effects in -layoutSubviews, and whenever configuring the cell, I called -setNeedsLayout to ensure -layoutSubviews was called.
Additionally, every time I setup my motion effects, I would remove the previous motion effects. This was key.
Here's the method I called in -layoutSubviews:
- (void)applyInterpolatingMotionEffectToView:(UIView *)view withParallaxLimit:(CGFloat)limit
{
NSArray *effects = view.motionEffects;
for (UIMotionEffect *motionEffect in effects)
{
[view removeMotionEffect:motionEffect];
}
UIInterpolatingMotionEffect *effectX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath: #"center.x" type: UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
effectX.minimumRelativeValue = #(-limit);
effectX.maximumRelativeValue = #(limit);
UIInterpolatingMotionEffect *effectY = [[UIInterpolatingMotionEffect alloc] initWithKeyPath: #"center.y" type: UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
effectY.minimumRelativeValue = #(-limit);
effectY.maximumRelativeValue = #(limit);
[view addMotionEffect: effectX];
[view addMotionEffect: effectY];
}
Hope it helps! Also, running on iOS 9.

UIImageView shadow not working

Am not sure where an wrong. I have an UIImageView in Storyboard, which am connecting with an IBOutlet to the code. I want to add a shadow (during runtime) to the UIImageView. And am using this code snippet for that (got this help from a stackoverflow post) :
-(void) awakeFromNib {
self.imageViewTopBar.layer.shadowColor = [UIColor blueColor].CGColor;
self.imageViewTopBar.layer.shadowOffset = CGSizeMake(0, 5);
self.imageViewTopBar.layer.shadowOpacity = 1.0;
self.imageViewTopBar.layer.shadowRadius = 3.0;
//self.imageViewTopBar.layer.masksToBounds = YES;
//self.imageViewTopBar.clipsToBounds = NO;
self.imageViewTopBar.layer.shouldRasterize = YES;
}
but it simply doesn't work. Any help please? I don't see any reason why it shouldn't work.
My environment : XCode 4.5.2, iOS 5, iPhone 4
You should use -viewDidLoad to do this, check out this post: Accessing View in awakeFromNib?
Try putting it into viewDidLoad. I assume you've included the quartzcore framework. I think it would throw compiler error if not...
Do you have checked, in the xib, clipToBounds to YES?
If YES you should uncheck the clipToBounds and reorganize your view hierarchy.
Normally when i have an imageView with a shadow i use this hierarchy
(aImageView) ClipToBounds=YES
|
|
(aView) Shadow setted here
|
|
(Superview)
aView has the same frame size of the aImageView

Resources