In Xamarin iOS, how can I simply move an image to the right 100px from its current location? I know that I am suppose to use Bounds, but I can't get the intenseness to really provide anything helpful. I have googled it and there isn't much that I can find.
Assuming you mean an UIImageView, you can use the Offset(dx,dy) method on its Frame property. If your UIImageView is called imageView:
var frame = imageView.Frame;
frame.Offset(100,0); // offset 100px horizontal, 0 px vertical
imageView.Frame = frame; // set the frame of the image to the new position.
Note that you must take the frame object into a seperate variable. That is, imageView.Frame.Offset(100,0) will not work.
Related
I must be missing something...why isn't this working? Instead of clipping to the circle the entire 800x800 backdrop image is displayed...
var mask;
var img;
function preload() {
game.load.image('back', 'backdrop.jpg');
}
function create() {
img = game.add.image(game.world.centerX, game.world.centerY,'back').anchor.setTo(0.5);
mask = game.add.graphics(0, 0);
mask.beginFill(0xffffff);
mask.drawCircle(game.world.centerX, game.world.centerY, 600);
img.mask = mask;
}
jsfiddle here
Disclaimer: I have no formal experience in phaser.io
I was able to fix this in your fiddle by changing
img = game.add.image(game.world.centerX, game.world.centerY,'back').anchor.setTo(0.5);
to
img = game.add.image(0, 0, 'back');
JSFiddle Fork
I would assume that placing the image in the centerX,centerY position results in the mask being offset of the image. Hopefully someone with more experience than I could explain the specifics here, but I will research further and update my answer as I figure out the why to go along with the how.
Update
Okay so I've done some digging through the documentation. First, you want to use img = game.add.image(0, 0, 'back'); due to the fact that the x and y parameters in this case dictate the upper-left origin of the image, not the center. By using game.world.centerX and game.world.centerY you are trying to throw the background image to the center of the canvas even though the canvas is the same size as the image.
using .anchor.setTo(0.5) from what I can gather, is attempting to set the anchor point at which the image originates to the centerX position. However, when you remove this anchor, suddenly the mask works, even though it is not showing correctly (because the position of the background image is incorrect).
Theory - By anchoring the image, I believe that it is no longer possible to apply a mask to it. By all experimenting that I've done, having an anchor set on the background image prevents it from being masked, so the mask simply is added as a child to img and is placed as it's center, thus why you are seeing the white circle instead of the circle properly masking the image.
it appears I was a mistaken about the fluidity of the api in trying to chain that last function call... if I break it up:
img = game.add.image(game.world.centerX, game.world.centerY, 'back');
img.anchor.setTo(0.5);
it now works!
Fiddle Here
I want to make speech bubbles but there is a problem adjusting size of the bubbles in UITextView.
is there a way to increase the size of the Textview automatically depending on the length of the
text by using UITextView?
Okay I found a better way of doing what I was suggesting. Rather then deal with the UIView and resizing depending on the text inside it. I just made the UITextView have a rounded edge which now looks like a panel. Perfect. Hope this helps someone!
If your interested in the code
- (void)drawRect:(CGRect)rect
{
CGRect frame = self.frame;
frame.size.height = self.contentSize.height;
self.frame = frame; // Drawing code
self.clipsToBounds = YES;
self.layer.cornerRadius = 10.0f;
}
I think I understand what your getting at. I had this problem the other day. This bit of code will adjust the UITextView contents. Create a new class and call this into it via a new class. If you want to adjust the View well I'm still working on that :-P
Hope this helps
- (void)drawRect:(CGRect)rect
{
CGRect frame = self.frame;
frame.size.height = self.contentSize.height;
self.frame = frame; // Drawing code
}
From top to bottom I have UIView, UIScrollView, a UIImage, a UILabel, a UITextView and a UIButton.
My reason behind the top-most UIScrollView was so the whole vertical content would scroll.
What I really need a substitute for is the UITextView (5th down) because the UITextView is a subclass of UIScrollView. And this substitute must accomodate the very tall column of formatted text.
What I don't want is a scrollable object in the middle of the page; I want the whole page to be scrollable.
One more thing ... please note there's a button immediately below this tall column of text.
John Love
THANK YOU! Ashack and Pentagp. I guess I finally had to surrender and dump the idea that IB could be used for all GUI ...
The entire UIView contains a UILabel, a UIImage, a UITextView and
a UIButton at the very bottom.
The goal here was to make this entire content scrollable.
Thanks to you guys at stackoverflow.com:
using IB, un-check "Scrolling Enabled" for the UITextView
because it's a concrete type of UIScrollView
using IB, drag a UIScrollView to the UIView and match sizes and
insure that the UIScrollView encloses all sub-views
make the UITextView height = its contentSize.height
move the UIButton to below this UITextView
and then, thanks to iphonedevsdk.com:
adjust the contentSize.height of the top-most UIScrollview to
include the height of the re-positioned UIButton
Problem solved!!!
Your question is not very specific, but I think you're asking how to make a text view that does not scroll, and expands to fit the text. You can do this with UILabel, but it's a multi-step process. The same will work for UITextView if you disable scrolling and set the contentSize to match the frame dimensions.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100,100,100,100)];
label.font = [UIFont systemFontOfSize:24];
NSString *yourTextString = #"your text";
CGSize maximumLabelSize = CGSizeMake(100, 500);
CGSize expectedLabelSize = [yourTextString sizeWithFont:label.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];
CGRect labelFrame = label.frame;
labelFrame.size.width = expectedLabelSize.width;
labelFrame.size.height = expectedLabelSize.height;
label.frame = labelFrame;
Then from there, use the label's frame to set the position of the button and all elements below it.
If I understand your problem, you don't want nested scroll.
So set yourTextView.scrollEnabled = NO; //Do it programmatically our in the nib
Also in order to display all the text set yourTextView frame height to its content height:
yourTextView.frame = CGRectMake(yourTextView.frame.origin.x,
yourTextView.frame.origin.y,
yourTextView.frame.size.width,
yourTextView.contentSize.height + (yourTextView.contentSize.width > yourTextView.frame.size.width ? (yourTextView.contentSize.width * yourTextView.contentSize.height / (yourTextView.contentSize.width - yourTextView.frame.size.width)) - yourTextView.contentSize.height :0)));
/*
If textView is dynamically filled, you have to check if yourTextView.contentSize.width is bigger than yourTextView.frame.size.width then add proportionally what is remaining to the height.
*/
At the end change your button position if needed:
yourbutton.frame = CGRectMake(yourbutton.frame.origin.x,
yourTextView.frame.origin.y + yourTextView.frame.size.height + 20, //If you want your button to be 20 point after your textView
yourbutton.frame.size.width,
yourbutton.frame.size.height);
Hope that will help you...
I'm getting crazy while trying to merge two UIImageView.
The situation:
a background UIImageView
(userPhotoImageView)
an overlayed
UIImageView (productPhotoImageView)
that can be streched, pinched and
rotated
I call my function on the UIImages but I can take coords and new stretched size from the UIImageView containing them (they are synthesized in my class)
- (UIImage *)mergeImage:(UIImage *)bottomImg withImage:(UIImage *)topImg;
Maybe simplest way would be rendering the layer of the top UIImageView in a the new CGContextRef like this:
[bottomImg drawInRect:CGRectMake(0, 0, bottomImg.size.width, bottomImg.size.height)];
[productPhotoImageView.layer renderInContext:ctx];
But in this way I loose the rotation effect previosly applied by gestures.
A second way would be trying to apply AffineTransformation to UIImage to reproduce GestureEffects and then draw it in the context like this:
UIImage * scaledTopImg = [topImg imageByScalingProportionallyToSize:productPhotoView.frame.size];
UIImage * rotatedScaledTopImg = [scaledTopImg imageRotatedByDegrees:ANGLE];
[rotatedScaledTopImg drawAtPoint:CGPointMake(productPhotoView.frame.origin.x, productPhotoView.frame.origin.y)];
The problem of this second approach is that I'm not able to exactly get the final rotation degrees (the ANGLE parameter that should be filled in the code above) amount since the user started to interact, this because the RotationGesture is reset to 0 after applying so the next callback is a delta from the current rotation.
For sure the most easy way would be the first one, freezing the two UIImageViews as they are displayed in that very moment but actually I still didn't find anyway to do it.
Ok basically there is another workaround for all this crazy merging stuff, but it's definitively not an elegant solution. For avoid handling any kind of AffineTransformation just capture the ImageScreen and then crop it.
CGImageRef screenImage = UIGetScreenImage();
CGRect fullRect = [[UIScreen mainScreen] applicationFrame];
CGImageRef saveCGImage = CGImageCreateWithImageInRect(screenImage, fullRect);
CGRect cropRect = CGRectMake(x,y,width,height);
CGImageRef saveCGImage = CGImageCreateWithImageInRect(screenImage, cropRect);
Told you it wasnt elegant, but for someone it could be useful.
Great that was helpful and so too much workaroundy ;)
so since i see it's pretty hard to find around some code examples to merge pics after a manipulation here goes mine, hope it can be helpful:
- (UIImage *)mergeImage:(UIImage *)bottomImg withImage:(UIImage *)topImg {
UIImage * scaledTopImg = [topImg imageByScalingProportionallyToSize:productPhotoView.frame.size];
UIGraphicsBeginImageContext(scaledTopImg.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, scaledTopImg.size.width * 0.5f, scaledTopImg.size.height * 0.5f);
CGFloat angle = atan2(productPhotoView.transform.b, productPhotoView.transform.a);
CGContextRotateCTM(ctx, angle);
[scaledTopImg drawInRect:CGRectMake(- scaledTopImg.size.width * 0.5f, -(scaledTopImg.size.height * 0.5f), scaledTopImg.size.width, scaledTopImg.size.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContext(bottomImg.size);
[bottomImg drawInRect:CGRectMake(0, 0, bottomImg.size.width, bottomImg.size.height)];
[newImage drawInRect:CGRectMake(productPhotoView.frame.origin.x, productPhotoView.frame.origin.y, newImage.size.width, newImage.size.height)];
UIImage *newImage2 = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage2;
}
Since I got a background image it was easier first create a context just to apply tranformation to the overlayview, than save it and write on top of the bottom layer.
Take care of the CTM translation before rotating the overlayview or it will rotate around an axis placed at {0,0} while I want to rotate my image around its center.
Regards
I have the same problem.
I only have problem with rotation.
I use gesture recognizers for move, scale and rotation.
At the beginning when I save the image tha scale was right, the position was right but never apply rotation to the saved image.
Now it works
if you want to know the rotation angle I get it with:
CGFloat angle = atan2(overlay.transform.b, overlay.transform.a);
And rotation with:
CGContextRotateCTM(context, angle);
If you have a different approach please let me know.
the problem I am having it that if inside the UIView Draw override, I change the view frame size, drawing a rectangle is not working as expected.
If I change the view frame size outside of the Draw override, it works fine. Is this an expected behavior or is it a problem with monotouch only?
This is the code I am using:
class ChildView : UIView
{
public override void Draw (RectangleF rect)
{
base.Draw (rect);
CGContext g = UIGraphics.GetCurrentContext();
//adding 30 points to view height
RectangleF rec = new RectangleF(this.Frame.Location,this.Frame.Size);
rec.Height+=30;
RectangleF rec_bounds = new RectangleF(0,0,rec.Width,rec.Height);
this.Frame=rec;
this.Bounds=rec_bounds;
//drawing a red rectangle to the first half of view height
UIColor.Red.SetFill();
RectangleF _rect = new RectangleF(this.Bounds.Location,this.Bounds.Size);
_rect.Height=_rect.Height/2;
g.FillRect(_rect);
}
}
However, the output of this code is this: (it should draw only 30 points red, but it draws 60 points)
Here is a link to download the project to reproduce this issue:
www.grbytes.com\downloads\RectangleDrawProblem.rar
Καλημέρα!
This behavior is expected. If you want to change the view's frame inside the Draw override, do it before getting the current context. That is because the graphics context also has a size and that is the size of the view at the time you are retrieving it.
Also, there is no need to set both the Bounds and the Frame of the view. You can just set either of them in this case.
By the way, I don't think you need to call base.Draw(). According to the Apple documentation, "If you subclass UIView directly, your implementation of this method does not need to call super."