offset a image view from another imageview? - iphone

I'm trying to set an imageviews origin to offset from another imaeviews origin but am having no luck a all.. I wanna be able to offset the other imageview so it doesn't appear directly ontop of it but a little to the left or right of the images center and up a little bit.
here's my code.
imageView1.center = CGPointMake(imageView2.center.x + 20, imageView2.center.y + 35);

probably easier to deal with origins...
kOffset = 5;
kWidth = 50
kHeight = 50;
CGRect frame = CGRectMake(0,0, kWidth, kHeight);
UIImageView *theImage;
for(int i = 0; i<numberOfImages; i++){
theImage = [[UIImageView alloc] initWithFrame:frame];
frame.origin.x += (kWidth + kOffset);
}

Related

UIScrollView with custom horizontal scrolling

I need to create an horizontal list with a scroll, this list is made of 10 images, in the screen only fits 1 and a half of these images, so when the user scroll i need to set at the center of the screen an image and besides it a portion on the forward image and backward image. So far what i have is this:
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 150, 1024, 520)];
scroll.delegate = self;
scroll.pagingEnabled = NO;
int numberOfViews = 10;
float xOrigin = 0;
for (int i = 0; i < numberOfViews; i++) {
xOrigin = i * 600;
FigureImage *tape = [[FigureImage alloc] initCustom];
[scroll addSubview:tape];
FigureImage *pictureFigure = [[FigureImage alloc] initCustom];
[scroll addSubview:pictureFigure];
}
scroll.contentSize = CGSizeMake(numberOfViews*10, 500);
pageWidth = (numberOfViews*10)/numberOfViews;
[self.view addSubview:scroll];
With this code i am able to scroll the images, but the scroll doesnt stop at any particular image, how can i able the last x position to stop the scrolling?
See the delegate method -scrollViewWillEndDragging:withVelocity:targetContentOffset:.
Its last parameter is an in-out CGPoint whose value you can change so that the scroll comes to rest with an image centered exactly.

Programmatically create UIButtons on CircleImage

I need to create Arch shaped UIButtons, on a circle image.(the O/p need to look like Concentric Circles),
At present I am using 5 images, but in future I may add some more Images, Dynamically I need to fil the circle Image with the added images.
I had a sample Piece of code and O/P is the below image
int numberOfSections = 6;
CGFloat angleSize = 2*M_PI/numberOfSections;
for (int j = 0; j < numberOfSections; ++j) {
UIButton *sectionLabel = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 2.0f)];
sectionLabel.backgroundColor = [UIColor redColor];
sectionLabel.layer.anchorPoint = CGPointMake(1.0f, 0.5f);
sectionLabel.layer.position = CGPointMake(container.bounds.size.width/2.0, container.bounds.size.height/2.0); // places anchorPoint of each label directly in the center of the circle.
sectionLabel.transform = CGAffineTransformMakeRotation(angleSize*j);
[container addSubview:sectionLabel];
}
I have tried with this piece of code and the O/P is the below image
int numberOfSections = 5;
CGFloat angleSize = 2*M_PI/numberOfSections;
for (int j = 0; j<numberOfSections; ++j) {
UIImage *imageframe = [imagesArray objectAtIndex:j];
OBShapedButton *button = [[OBShapedButton alloc] initWithFrame:CGRectMake(0.0f, 150.0f, 150.0f, 128.0f)];
button.transform = CGAffineTransformMakeRotation(angleSize*j);
[container addSubview:button];
}
I need my output as the below image
How can i Achieve that
O/P after I Tried with Sarah Zuo's code
same width and height buttons
AnyKind of Help is more Appreciated
I can answer only last part,
If you are able to get images for buttons, then you can refer this tutorial. This might help you. Provided, your image formed is having transparent background.
float radius = container.bounds.size.width / 2;
int numberOfSections = 5;
CGFloat angleSize = 2*M_PI/numberOfSections;
for (int j = 0; j<numberOfSections; ++j) {
UIImage *imageframe = [imagesArray objectAtIndex:j];
OBShapedButton *button = [[OBShapedButton alloc] initWithFrame:CGRectMake(0.0f, 150.0f, 150.0f, 128.0f)];
button.transform = CGAffineTransformMakeRotation(2*M_PI-angleSize*j);
button.center = CGPointMake(radius + radius * sin(angleSize * j) / 2, radius + radius * cos(angleSize * j) / 2);
[container addSubview:button];
}
Try this code.
You can use your UIImageView instead. You can give different tag to image view and add Gesture Recognizer. Now you can get touch events like button click event on image views.
If all buttons' image look like the one (same direction), the transform value may be work.
button.transform = CGAffineTransformMakeRotation(2*M_PI-angleSize*j);
button.center = CGPointMake(radius + radius * sin(angleSize * j) / 2, radius + radius * cos(angleSize * j) / 2);

How to modify the position of UIImageViews subviews inside a UIScrollView?

I'm trying to create gaps between the UIImageViews, which are subviews added into a UIScrollView.
And I thought I could do the following by modifying the CGRect of the UIImageView.
CGRect frame = imageView.frame;
frame.origin.x = <some values>
frame.origin.y = 0
imageView.frame = frame;
By modifying the x values, I assumed it will help me with the position of the UIImageViews inside the UIScrollView.
However, I realized whichever values I set for the frame.origin.x does not really matter. As each UIImageView inside the UIScrollView will be positioned side by side without any empty space.
I think you're doing it in the wrong way. You can't edit view's position by
view.frame.origin.x = something;
You should assign a new frame, like this:
view.frame = CGRectMake(x, y, w, h);
In your case it will look like this:
CGRect frame = imageView.frame;
CGFloat frameX = frame.origin.x;
CGFloat frameY = frame.origin.y;
CGFloat frameW = frame.size.width;
CGFloat frameH = frame.size.height;
frameX = <some values>
frameY = 0;
imageView.frame = CGRectMake(frameX, frameY, frameW, frameH);

UIAlertview Button columns

In iphone application can we set number of columns in UIAlertView as if I have six buttons in UIAlertView then how can I show it in 2 columns and 3 rows. If any one done it kindly share it how it will done ..
Any sample code will be extra help
A UIAlertView is just a UIView. So you can manually add the buttons to the UIAlertView in a two column configuration. There is an example here that demonstrates adding UITextFields, but I'm sure you can adapt it.
Note that having two many buttons in the UIAlertView might get Apple's back up ;-)
I have done it like this
NSArray *subViewArray = Topicdialog.subviews;
float y = 60.0f;
for(int x=2;x<[subViewArray count];x += 2){
UIView *button = [subViewArray objectAtIndex:x];
CGRect frame = button.frame;
frame.size.width = 120.0f;
frame.size.height = 42.0f;
frame.origin.x = 20.0f;
frame.origin.y = y;
button.frame = frame;
UIView *button1 = [subViewArray objectAtIndex:x + 1];
frame = button1.frame;
frame.size.width = 120.0f;
frame.size.height = 42.0f;
frame.origin.x = 152.0f;
frame.origin.y = y;
button1.frame = frame;
y = y + 48.0f;
}

Get the identity of the image loaded at the centre of the screen currently?

I have a uiscrollview with contentsize 1280*1280 and 25 images of equal size loaded in it. When i scroll and meet an end, I want to know the identity of the image which is currently present at the screen centre. Given below is the code that i used to load the images into the scrollView. How can i figure it out?
for (i = 0; i < 5; i++) {
if(j == 5)
{
tx = tx+1;
x=x+256;
ty = ty+5;
y = y-1280;
}
for (j = 0; j < 5; j++) {
imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://stage.discoveritalia.it/tiles/%d/%d/%d.png",zoom,tx,ty]]]]];
frame = [imageView frame];
frame.origin.x = x;
frame.origin.y = y;
[imageView setFrame:frame];
[myScrollView addSubview:imageView];
ty = ty-1;
y = y+256;
[imageArray insertObject:imageView atIndex:i*5+j];
[imageView release];
}
}
If I could get the URL back, it would solve my problem right away. Is there a way to do it?
Calculate the CGPoint at the center of the screen (depending on orientation)
Divide the x and y values by 1280 / 5 and add ((1280 / 5) / 2) to determine the tile i-j indices that the center would overlap, with no UIScrollView offset
Now add the current scroll view offset to determine which tile the center actually overlaps