I tried to move a ball image in viewDidLoad as below. I ran it. The Log showed the x value changed from 160 to 50 as I expected. However, the image on the iOS Simulator is still on the original position from the Interface Builder in which x is 160. Why?
And how can I move the location of the image before it shows on the screen?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"%f", Ball.center.x);
Ball.center = CGPointMake(50, 50);
NSLog(#"%f", Ball.center.x);
}
Instead of viewDidLoad add your code to viewDidLayoutSubviews method
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
Ball.center = CGPointMake(50, 50);
}
Call [self.view setNeedsDisplay]; method , since your viewDidLoad triggers after the view is actually loaded and drawn on the device , and you changing the image location you should tell your device to redraw the view with a new image position.
A couple of suggestions:
1. First check that `Ball` is connected to IBOutlet in interface builder.
2. To change the frame of a UIImageView, I would suggest to use UIView animation
blocks for smooth animation effects
3. You can also use `performSelector` and delay the animation to e.g., `0.1` sec etc.
Hope these point can guide you to resolve this issue.
Related
so there must be something simple I am missing because I just can't figure out how to move my UIView
This is what I have so far
my .m file
- (void)viewDidLoad
{
[super viewDidLoad];
[self buttonLayout];
}
-(void)buttonLayout
{
UIView *theView = [self buttonGroup];
theView.center=CGPointMake(50, 50);
}
My .h file
#property (nonatomic, retain) IBOutlet UIView *buttonGroup;
So this is what I have so far and i just cant seem to get it to move at all
P.S i don't want it animated as i'm getting my layout to move to compensate for different iPhone screen sizes :)
I have not tested this, but it seems like you need to:
A) Remove the view from its parent.
B) Adjust the center of its 'frame', not the view.
C) Re-add the subview.
Like so:
UIView *theView = [self buttonGroup];
CGRect theFrame = theView.frame;
[theView removeFromSuperview];
theFrame.origin.x = 50.0;
theFrame.origin.y = 50.0;
theView.frame = theFrame;
[self.view addSubview:theView];
This should work with autolayout.
As the other poster says, you can't move views around by changing their center or frame when you have auto-layout in effect.
Instead, you have to add a vertical and horizontal constraint that positions the view from the top/left edge, connect an outlet to the constraint, and then change the constant value on the constraint from code.
I had similar problem , but when i tried to change the origin as suggested in first solution , i got "Expression not assignable" error.
so i solved it with below solution in viewDidLoad().
First save its center in some temp variable , then change the centre and add again to its parent view.
CGPoint buttonCenter = self.yourButton.center;
[self.yourButton removeFromSuperview];
buttonCenter.y = buttonCenter.y + 50.0;
self.yourButton.center = buttonCenter;
[self.view addSubview:self.yourButton];
I have created a UIButton programmatically and horizontally centered it using the following code
button.center = CGPointMake(self.view.center.x, 50);
When device is rotated, it's no longer in the center. How can I fix this problem? Thanks in advance.
Kevin is correct, but a better solution would be to set the button's center in your view controller's willAnimateRotationToInterfaceOrientation:duration: method.
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
button.center = CGPointMake(self.view.center.x, 50);
}
He uses the didRotateFromInterfaceOrientation:fromInterfaceOrientation method, which, as the Apple documentation states "this method might be used to reenable view interactions, start media playback again, or turn on expensive drawing or live updates."
In the case of laying out subviews, the willAnimateRotationToInterfaceOrientation:duration: will provide a smoother transition, as it is called from within the rotation's animation block.
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
button.center = CGPointMake(self.view.center.x, 50);
}
I'm attempting to create a very simple parallax effect when the user scrolls a UIScrollView. I'm using the scrollViewDidScroll method on my scrollview, thats all working. I can log an everything from that method so I know it is being fired off when the UIScrollView is scrolled. The issue is any changes I attempt to make to the contents of UIScrollView fails. Possibly a rendering issue? Does a UIScrollView not re-render its contents during scrolling?
I have tried changing the UIImageView(imageView)'s frame and center point, nothing is working. Any ideas on what might be going on, I'm not getting any sort of errors or anything.
-(void)scrollViewDidScroll:(UIScrollView *)body {
float y = body.contentOffset.y;
imageView.center = CGPointMake(imageView.bounds.size.width/2, (imageView.bounds.size.height/2) - y/2);
// Tried this as well
//[imageView setFrame:CGRectMake(0, 0, imageView.bounds.size.width, imageView.bounds.size.height + (y * 2))];
NSLog(#"We are scrolling...");
}
Figured it out, I actually needed to grab the instance of the ImageView within the ScrollView using. Anyone else trying to figure this out, I did it a little hacky but it works.
/* Paralax Scrolling */
-(void)scrollViewDidScroll:(UIScrollView *)body {
float y = body.contentOffset.y;
// When adding (body) image view to scroll view I assigned a tag of 100 to it.
[body viewWithTag:100].center = CGPointMake([body viewWithTag:100].bounds.size.width/2, ([body viewWithTag:100].bounds.size.height/2) + y/2);
}
what I am having is
UIView
1.Image1
2.Image2
3.UILabel
like an image below
Then I apply rotation on UILabel by doing
- (void)viewDidLoad
{
testLabel.transform = CGAffineTransformMakeRotation(M_PI*0.25);
[super viewDidLoad];
}
and when I am running the application, the uilabe is disppearing after all. Look at the second image for your reference
Please point out what I am doing wrong here....and how to get the work done
Thanks
Watch out because Autolayout cause a lot of problems.
Try to deselect 'Use Autolayout'
It solves to me all the problems trying to translate objects.
Try multiplying by a smaller value against PI to see if it is rotating or just disappearing. If I remember correctly, rotations are not based on the center, but on the top-left corner, so you have to translate afterwards!
For instance, to rotate a video clip this is what I had to do:
CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI);
CGAffineTransform translateToCenter = CGAffineTransformMakeTranslation(640, 480);
CGAffineTransform mixedTransform = CGAffineTransformConcat(rotation, translateToCenter);
[firstTrackInstruction setTransform:mixedTransform atTime:kCMTimeZero];
I rotated by PI first (180 degrees), but because the center of rotation is the top left corner, my video clip was now in the opposite quadrant, and needed to be transformed back! This may be what is happening with your label.
So try this, assuming your label is 42x21 dimensions..
CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI);
CGAffineTransform translateToCenter = CGAffineTransformMakeTranslation(42, 21);
CGAffineTransform mixedTransform = CGAffineTransformConcat(rotation, translateToCenter);
label.transform = mixedTransform;
try putting the [super viewDidLoad] first :
- (void)viewDidLoad
{
[super viewDidLoad];
testLabel.transform = CGAffineTransformMakeRotation(M_PI*0.25);
}
AutoLayout and AutoResizing often cause difficulties when you apply transforms as these seem to alter the frame rather than the bounds and center of a view. Hence the automatic adjustments wreck havoc on your layout. Try to wrap the transformed label in a view that does not change dimensions and layout that view within its superview however you want.
i am trying to remake (mostly) the same sample example provided by apple which is called tapToZoom.
the difference is that i am using storyboard instead of a classic nib file as the example shows, i set an image inside the scrollView.
the problem is that the zoom don't work e.g. here:
//set the initial zoom scale
float minimumScale = [myScroll frame].size.width / [myImage frame].size.width;
NSLog(#"%f",minimumScale);
[myScroll setMinimumZoomScale:minimumScale];
[myScroll setZoomScale:minimumScale];
NSLog()#"%f",[myScroll zoomScale];
the logs return 0.6 for the first one and 1.0 for the second which is supposed to be 0.6.
anyone got this problem before?
[EDIT]: the previous lines are into my viewDidLoad method.
I think you should implement
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
Try this delegate method.