I am working on an app where my task is to get url-links from the database that i created a scroll view and placed images on the scroll. I have written some code that when i click on the image i have to show the url page in a webview which i placed above the scroll view. For this i wrote touch code for getting the url when i click on the image but i got problem like my click is not working on the image and scrollview also but it is working on the down part of the image and scrollview which is a normalview. U can find my touch code below
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [[event allTouches] anyObject];
for(int index=0;index<[array count];index++)
{
UIImageView *imgView = [array objectAtIndex:index];
if(CGRectContainsPoint([imgView frame], [touch locationInView:scrollView]))
{
NSString *strgetcontenturl = [[NSString alloc]initWithString:[arrayvideoFile objectAtIndex:index]];
NSURL *url = [NSURL URLWithString:strgetcontenturl];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strgetcontenturl]]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webview loadRequest:requestObj];
}}}
Here array is where i stored all the images init and arrayvideoFile is where I stored all the urls.
Please help.
How did you placed your images to your scollview? ImageView?
I would place them with a UIButton (CustomType), then you can add target to that button and no need to detect touches. Because an UIButton already detects the touch events.
UIButton *button = [UIButton butttonWithType:UIButtonTypeCustom];
UIImage * buttonImage = ....
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(x, y, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self
action:#selector(buttonAction:)
forControlEvents:UIControlEventTouchUpInside];
[scrollview addSubview:button];
Edit: Added code
Use below code.
for(int Tag =0; Tag < [array count]; Tag++) {
UIButton *btn_URL = [UIButton butttonWithType:UIButtonTypeCustom];
UIImage * btn_URL = [btn_URL setImage:buttonImage forState:UIControlStateNormal];
btn_URL.frame = CGRectMake(x, y, buttonImage.size.width, buttonImage.size.height);
[btn_URL addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
btn_URL.tag = Tag;
[scrollview addSubview:btn_URL];
}
-(void)buttonAction:(id)sender {
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:#"%#",[array objectAtIndex:[sender Tag]]]];
}
Here you can redirect user to URL.
If you need any help then let me know.
Thanks,
Hemang.
just set imgView.userInteractionEnabled=YES; it may help you.
otherwise use this code
UIScrollView *scrollview4preview=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-44)];
scrollview4preview.backgroundColor=[UIColor clearColor];
[self.view addSubview:scrollview4preview];
int yval=0;
for (int i=0; i<[arrary count]; i++) {
UIButton *btn4preview=[[UIButton alloc]init];
btn4preview.tag=i;
btn4preview.frame=CGRectMake(scrollview4preview.frame.origin.x+10, i*yval, 150, 150);
[btn4preview setImage:[UIImage imageNamed:#"YourImageName.png"] forState:UIControlStateNormal];
[btn4preview addTarget:self action:#selector(btnPreviewSelect:) forControlEvents:UIControlEventTouchUpInside];
btn4preview.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin;
[scrollview4preview addSubview:btn4preview];
}
scrollview4preview.contentSize=CGSizeMake(scrollview4preview.frame.size.width, yval+160);
-(void)btnPreviewSelect:(id)sender
{
UIButton *btn=(UIButton *)sender;
NSString *strgetcontenturl = [[NSString alloc]initWithString:[arrary objectAtIndex:btn.tag]];
NSURL *url = [NSURL URLWithString:strgetcontenturl];
[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strgetcontenturl]]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webview loadRequest:requestObj];
}
Related
I have a large amount of image buttons being added to the screen in an iPhone app. Everything is working fine with my code.
The problem is the subviews are not displaying until every one of the subviews are ready to display. So for example I have 48 subviews, I don't want to wait until every subview is ready, I want to visibly be able to see each one appearing on the screen as they are loading.
I hope this makes sense. The issue is getting larger as i add more images to the collection.
This is my code for you to see:
for (int i = 0; i < imgNumbers.count; i++) {
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect buttonValue = [[btn9Locations objectAtIndex:location] CGRectValue];
myButton.frame = buttonValue;
NSString *imagePart1 = [[NSString alloc] initWithFormat:#"%#", imgNumbers[i]];
[myButton addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchDownRepeat];
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSURL *fileURL = [tmpDirURL URLByAppendingPathComponent:imagePart1];
NSString *imageLocation = [[NSString alloc]initWithFormat:#"%#", [fileURL path]];
UIImage * image = [UIImage imageWithContentsOfFile:imageLocation];
[myButton setBackgroundColor:[UIColor colorWithRed:0.94 green:0.39 blue:0.13 alpha:0.0]];
[myButton setBackgroundImage:image forState:UIControlStateNormal];
[myButton setTitle:[imageNames[i] uppercaseString] forState:UIControlStateNormal];
[myButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Light" size:20]];
[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
[myButton setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
[myButton setTitleColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5] forState:UIControlStateNormal];
myButton.imageView.layer.cornerRadius = 7.0f;
myButton.layer.shadowRadius = 3.0f;
myButton.layer.shadowColor = [UIColor blackColor].CGColor;
myButton.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
myButton.layer.shadowOpacity = 0.5f;
myButton.layer.masksToBounds = NO;
[buttons addObject:myButton];
[scrollview addSubview:myButton]; }
Hope this makes sense, I have seen apps that when you scroll down each item fades onto the screen but I don't seem to be able to sort this part first!
Thanks
Try to move initialisation code into background queue and only addSubview call on main queue.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
for (int i = 0; i < imgNumbers.count; i++) {
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect buttonValue = [[btn9Locations objectAtIndex:location] CGRectValue];
myButton.frame = buttonValue;
NSString *imagePart1 = [[NSString alloc] initWithFormat:#"%#", imgNumbers[i]];
[myButton addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchDownRepeat];
NSURL *tmpDirURL = [NSURL fileURLWithPath:NSTemporaryDirectory() isDirectory:YES];
NSURL *fileURL = [tmpDirURL URLByAppendingPathComponent:imagePart1];
NSString *imageLocation = [[NSString alloc]initWithFormat:#"%#", [fileURL path]];
UIImage * image = [UIImage imageWithContentsOfFile:imageLocation];
[myButton setBackgroundColor:[UIColor colorWithRed:0.94 green:0.39 blue:0.13 alpha:0.0]];
[myButton setBackgroundImage:image forState:UIControlStateNormal];
[myButton setTitle:[imageNames[i] uppercaseString] forState:UIControlStateNormal];
[myButton.titleLabel setFont:[UIFont fontWithName:#"Helvetica-Light" size:20]];
[myButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
[myButton setContentVerticalAlignment:UIControlContentVerticalAlignmentBottom];
[myButton setTitleColor:[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5] forState:UIControlStateNormal];
myButton.imageView.layer.cornerRadius = 7.0f;
myButton.layer.shadowRadius = 3.0f;
myButton.layer.shadowColor = [UIColor blackColor].CGColor;
myButton.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
myButton.layer.shadowOpacity = 0.5f;
myButton.layer.masksToBounds = NO;
[buttons addObject:myButton];
dispatch_async(dispatch_get_main_queue(), ^{
[scrollview addSubview:myButton];
});
}
});
Embeded Youtube video in UIWebview. How can i play this you tube video using UIWebview when UIButton is pressed.
- (void)viewDidLoad
{
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize screenSize = rect.size;
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)];
webView.autoresizesSubviews = YES;
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
NSString *videoUrl = #"http://http://www.youtube.com/v/oHg5SJYRHA0";
NSString *htmlString = [NSString stringWithFormat:#"<html><head><meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head><body style=\"background:#F00;margin-top:0px;margin-left:0px\"><div><object width=\"320\" height=\"480\"><param name=\"movie\" value=\"%#\"></param><param name=\"wmode\" value=\"transparent\"></param><embed src=\"%#\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"320\" height=\"480\"></embed></object></div></body></html>",videoUrl,videoUrl] ;
[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://www.youtube.com"]];
//[window addSubview:webView];
[webView release];
[super viewDidLoad];
// Do any additional setup after loading the view.
}
When user presses this UIButton it should start playing embedded youtube video
UIButton *videoButton = [UIButton buttonWithType:UIButtonTypeCustom];
[videoButton addTarget:self action:#selector(VideoAction:) forControlEvents:UIControlEventTouchUpInside];
videoButton.frame = CGRectMake(0, 0, 40, 40);
UIImage *icn = [UIImage imageNamed:#"Video_icon.png"];
[videoButton setImage:icn forState:UIControlStateNormal];
UIBarButtonItem *video = [[UIBarButtonItem alloc] initWithCustomView:videoButton];
How can i play youtube video.
Thanks for help.
Why dont you try this :
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.youtube.com/watch?v=oHg5SJYRHA0"]]]
[self.view addSubview:webView];
I have to implement a button with rectangular border and it should be like this
and I am using the following code to do that:
-(void)createDynamicView:(CGRect)frame{
UIButton *drugimgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
drugimgBtn.frame = frame;//CGRectMake(0, 0, 155, 130);
NSString *myurl = responseDrugInfo.ThumbImg; //by rajesh
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myurl]];
UIImage *tbImage = [[UIImage alloc] initWithData:imageData];
[drugimgBtn setBackgroundImage:tbImage forState:UIControlStateNormal];
[drugimgBtn addTarget:self action:#selector(imageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
UIView * prescView = [[UIView alloc]init];
prescView.frame = frame;
prescView.layer.borderColor = [UIColor lightGrayColor].CGColor;
prescView.layer.borderWidth = 1.0f;
[prescView addSubview:drugimgBtn];
[scrollview addSubview:drugimgBtn];
//[prescView release];
[myurl release];
[tbImage release];
}
The output is as follow:
How do I implement a rectangular border so it can appeared like the same in the first image?
I'm new to Objective C. I created the method to construct & display a button inside a UIView (UIView is inside another View named contentView, and contentView to be added as a subview in a UIScrollView. Well the problem is that I cannot click on the generated button to trigger the IB action named playAction. Can someone pls help me? Thanks
- (void) displayCategoryBestSellers
{
//Structure: ScrollView -> ContentView -> MainView -> ImageView and Button
UIView * mainView = [[UIView alloc] initWithFrame:CGRectMake(0,0,160,105)];
mainView.userInteractionEnabled = YES;
UIImage * backgroundImage = [UIImage imageNamed:#"blackwhitesquare.png"];
UIImageView * uiImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,145,105)];
uiImageView.image = backgroundImage;
uiImageView.center = CGPointMake(mainView.frame.size.width /2, mainView.frame.size.height/2);
uiImageView.userInteractionEnabled = YES;
[mainView addSubview:uiImageView];
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
playButton.frame = CGRectMake(0,0,100,90);
NSData *mydata = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:#"http://media.theseus.cataloguesolutions.com/images/72by72/324178611_0004_PG_1.jpg"]];
UIImage *myimage = [[UIImage alloc] initWithData:mydata];
[playButton setBackgroundImage:myimage forState:UIControlStateNormal];
playButton.center = CGPointMake(mainView.frame.size.width /2, mainView.frame.size.height/2);
[playButton addTarget:self action:#selector(playAction:) forControlEvents:UIControlEventTouchDown];
playButton.userInteractionEnabled = YES;
[mainView addSubview:playButton];
[contentView addSubview:mainView];
contentView.userInteractionEnabled = YES;
[scrollView addSubview:contentView];
scrollView.delegate = self;
scrollView.userInteractionEnabled = YES;
}
-(IBAction)playAction: (id)sender
{
NSLog(#"Button Clicked");
}
Use UIControlEventTouchUpInside instead of UIControlEventTouchDown.
I have a windowed (e.g. not a full-screen) UIScrollView (inside a UIView) which scrolls through groups of UIImageViews with UIButtons on them (the idea being you click the button to do something with the displayed image). The UIButton does take any touch events - how can I fix it?
I've read this, this, this, this and this - but I either don't understand the answer or its implications, or it's not really relevant.
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
Sentence *testSentence = [[managedObjectContext executeFetchRequest:request error:&error] objectAtIndex:i];
//NSLog(#"testSentence: %#", testSentence);
//NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", i];
//Your going to need to optimise this by creating another thumbnail image to use here.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *imageName = [[paths objectAtIndex:0] stringByAppendingPathComponent:[testSentence image]];
//NSLog(#"imageName: %#", imageName);
UIImage *image = [[UIImage alloc] initWithContentsOfFile:imageName];
//NSLog(#"image: %#", image);
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
//NSLog(#"imageView: %#", imageView);
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[imageView setBackgroundColor:[UIColor blackColor]];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i; // tag our images for later use when we place them in serial fashion
imageView.frame = rect;
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[aButton setTitle:#"Play" forState:UIControlStateNormal];
aButton.frame = CGRectMake(10, 10, 70, 70);
[aButton setUserInteractionEnabled:YES];
[aButton setEnabled:YES];
[aButton setAlpha:1];
UIView *buttonWrapperUIView = [[UIView alloc] init];
[aButton addTarget:self action:#selector(clickPlay:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:aButton];
[imageView bringSubviewToFront:aButton];
[scrollView1 addSubview:imageView];
NSLog(#"aButton %#", aButton);
[image release];
[imageView release];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
try
[imageView setUserInteractionEnabled:YES]