I am not a very good programmer so if this is a stupid question forgive me !
I am just learning image transformation in ios
i want to translate a image from one position to another on button click event
Here is the code…
- (IBAction)showinfobybutton:(id)sender {
CGFloat v1 = imageview.center.x;
CGFloat v2 = imageview.center.y;
CGAffineTransform trans = CGAffineTransformTranslate(imageview.transform, v1, v2);
imageview.transform = trans;
}
What’s wrong with the code. it only translate in y direction not in x. no matter how much you put the value for v1 function don’t care. any idea what’s going on …..
I am totally stucked
Thank You for any help
In terms of your question about
CGAffineTransform, a couple of thoughts:Are you doing this in
initmethod orviewDidLoad? If so, that may be too early in the view creation process as many of theUIViewrelated properties of theimageview(such ascenter) are not reliable at that point. Take a look and make sure you’re getting non-zero values forv1andv2. If you want to look at frame coordinates or the like, you might want to defer that untilviewDidAppearor later.Are you using autolayout? Open up the storyboard or NIB and look at the document properties (below). If you’re moving things around on the screen, you might want to turn that off.
As an aside, your setting
v1andv2based upon thecenterof theimageviewis curious. You’re moving thecenterby the amount of thecenter. Usually you’d see something like the following:By the way, I animate (a) because that’s often a better user experience; and (b) when doing diagnostics like this, it makes it easier for me to see what it’s changing from and what it’s changing to. If you don’t want animation, obviously don’t use
animateWithDurationcode.By the way, if you want to move a
UIViewcontrol, if you’re not using autolayout, the most common way is to useanimationWithDurationto change either thecenteror theframe, e.g.:If you’re using autolayout, the process is a little more complicated. One technique in autolayout is to programmatically change the constraints, but that’s probably beyond the scope of this question.