I am doing app related to custom alert. Written code for displaying alert view with transparent background when i click ok button on alert-view an alert disappears.
I need help for same thing happen while i touch the transparent view also my code is a below
:
- (void)didCustomPopUpAlertLoad:(UIView *)parentView andtitle:(NSString *)strTitle {
[self setRootView:parentView];
self.lblAlertMessage.text = strTitle;
//Add alertview into transparent view to hide parent view interaction
UIView *transparentView = [[UIView alloc] initWithFrame:parentView.bounds];
[transparentView setBackgroundColor:[UIColor clearColor]];
[transparentView addSubview:self];
float x = (int)(transparentView.bounds.size.width - self.bounds.size.width)>>1;
float y = (int)(transparentView.bounds.size.height - self.bounds.size.height)>>2;
[self setFrame:CGRectMake(x, y+62, self.bounds.size.width, self.bounds.size.height)];
// [self setFrame:CGRectMake(x+10, y+62, self.bounds.size.width, self.bounds.size.height)];
[self.window addSubview:transparentView];
[self.window makeKeyAndVisible];
[[transparentView subviews]
makeObjectsPerformSelector:#selector(setUserInteractionEnabled:)
withObject:[NSNumber numberWithBool:FALSE]];
}
-(void)didCustomPopUpUnload{
[self.superview removeFromSuperview];
// Set up the fade-in animation
self.window = nil;
}
-(IBAction)didActionOkAlertPopUp:(id)sender{
[self didCustomPopUpUnload];
}
Create a custom transparent view then override this method
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
Add your alert view into this view and use this view as full screen view.
You can use event param to calculate to know user touch inside or outside of alertview.
Related
I want to touch on UIImageView and that image display in another view in full size using navigate the view. same as gallery in mobile.
just creating demo programmatically. because I'm new in iphone developing.
Here is my code.
- (void)viewDidLoad
{
NSLog(#"Enter in viewDidLoad method");
UIScrollView * scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, self.view.frame.size.height+50);
[self.view setUserInteractionEnabled:YES];
[scroll setUserInteractionEnabled:YES];
[self.view addSubview:scroll];
int x=5;
int y=5;
int hei=100;
int wid=100;
int k=1;
for (int i=0; i<3; i++)
{
for(int j=0;j<50;j++)
{
imageView = [[UIImageView alloc]initWithFrame:CGRectMake((x+wid)*i+5, (y+hei)*j+5, wid, hei)];
[imageView setImage:[UIImage imageNamed:#"Motocross.png"]];
[imageView setUserInteractionEnabled:YES];
scroll.contentSize = CGSizeMake(320,(y+hei)*j+hei*2);
[imageView setTag:k];
[scroll addSubview:imageView];
NSLog(#"The tag is == %d",k++);
NSLog(#" (%d,%d)x == %d",i,j,(x+wid)*i+5);
NSLog(#" (%d,%d)y == %d",i,j,(y+hei)*j+5);
}
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"Exit from viewDidLoad method");
}
//Navigate on touch the imageView.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"Enter in touchesBeganWithEvent method");
UITouch *touch = [touches anyObject];
if ([touch view] == imageView)
{
NSLog(#"Enter in if condition of touch");
DetailViewController *detailViewController = [[DetailViewController alloc]initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.imageD = imageView.image;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
NSLog(#"Exit from if condition of touch");
}
NSLog(#"Exit from touchesBeganWithEvent method");
}
I want to tap on imageView and view will navigate to detailViewController and display that image in big size. but this touchesBegan method not called when i tap on imageView. In this i'm not creating array of UIImageView just alloc init in loop.. Thank you!
Rather than using touchesBegan: on the view, consider adding a UITapGestureRecognizer to each image view. When the image view is tapped (you are setting userInteractionEnabled already which is good) then you can get the view that was tapped with sender.view.
Note that you create a new instance of UITapGestureRecognizer for each image view that you have, but that the target/action of each gesture can be the same.
The problem with your current code is that this check:
if ([touch view] == imageView)
verifies the touch only against the last image view that you added to the scroll view. So, touching the last one should work, but all the others won't.
As per the comment from #vin, the correct solution to that issue is to verify the class type of the view that was touched:
if ([[touch view] isKindOfClass:[UIImageView class]]) {
(though the scroll view may have other image view subclasses than just the ones you add...)
Try bellow code or its better to use Collection view
- (void)viewDidLoad
{
NSLog(#"Enter in viewDidLoad method");
UIScrollView * scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scroll.contentSize = CGSizeMake(320, self.view.frame.size.height+50);
[self.view setUserInteractionEnabled:YES];
[scroll setUserInteractionEnabled:YES];
[self.view addSubview:scroll];
int x=5;
int y=5;
int hei=100;
int wid=100;
int k=1;
for (int i=0; i<3; i++)
{
for(int j=0;j<50;j++)
{
UIButton *buttonForImage=[[UIButton alloc]initWithFrame:CGRectMake((x+wid)*i+5, (y+hei)*j+5, wid, hei)];
[buttonForImage setImage:[UIImage imageNamed:#"Motocross.png"] forState:UIControlStateNormal];
[buttonForImage addTarget:self action:#selector(imageButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
scroll.contentSize = CGSizeMake(320,(y+hei)*j+hei*2);
[buttonForImage setTag:k];
[scroll addSubview:buttonForImage];
NSLog(#"The tag is == %d",k++);
NSLog(#" (%d,%d)x == %d",i,j,(x+wid)*i+5);
NSLog(#" (%d,%d)y == %d",i,j,(y+hei)*j+5);
}
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(#"Exit from viewDidLoad method");
}
//Navigate on touch the imageView.
-(void)imageButtonPressed:(UIButton*)sender{
NSLog(#"Enter in if condition of touch");
DetailViewController *detailViewController = [[DetailViewController alloc]initWithNibName:#"DetailViewController" bundle:nil];
detailViewController.imageD = sender.imageView.image;
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
You need to use UICollectionview rather than trying to juggle with rows /colns, images touches etc...
Good tutorial is here... UICollectionView Tutorial
(Assuming you dont support below iOS 6.0)
In iOS 7, when a UIActionSheet is presented, the system controls all animate their tintColor to gray. When the sheet is dismissed, they animate back.
I have some controls with custom background images or drawRect implementations that use the tint color, and I'd like them to animate that change just like the system controls.
Apple added - (void)tintColorDidChange to UIView, but redrawing with the new color in this method doesn't animate the change — it just switches from full color to full gray immediately, which looks bad when other system controls around it are animating.
How can I make my custom-drawn controls animate the tintColor transition like Apple's?
You can do this with a Core Animation transition applied to the view layer:
//set up transition
CATransition *transition = [CATransition animation];
transition.type = kCATransitionFade;
transition.duration = 0.4;
[self.layer addAnimation:transition forKey:nil];
//now trigger a redraw of the background using the new colour
//and the transition will cover the redraw with a crossfade
self.flagToIndicateColorShouldChange = YES;
[self setNeedsDisplay];
EDIT: there may be a race condition between the animation and the redraw depending on exactly how you do the redraw. If you can post the original redraw code you tried that updated immediately (without animation) I can provide a more specific answer.
Have you tried tintAdjustmentMode?
You should watch WWDC 2013 Session 214 Customizing Your App’s Appearance for iOS 7 and read the TicTacToe demo app
Something like this
- (void)onPopupOpened
{
[UIView animateWithDuration:0.3 animations:^{
window.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed;
}];
popupView.tintAdjustmentMode = UIViewTintAdjustmentModeNormal;
popupView.tintColor = [UIColor redColor];
}
- (void)onPopupClosed
{
[UIView animateWithDuration:0.3 animations:^{
window.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic;
}];
}
Shouldn't drawRect: be called automatically for the animation to update for the new tintColor?
I've made a demo application that has three controls in the main view controller. The first is a button that brings up a standard action sheet. The second is a button that's there for observation (the tapped button is hard to compare to during the animation). The third is a custom UIView subclass that simply draws a rectangle of the view's tintColor. When tintColorDidChange is called, I call setNeedsDisplay, which in turn will call drawRect:.
I created a new application with one view controller:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[UIApplication sharedApplication] keyWindow].tintColor = [UIColor blueColor];
// Button to bring up action sheet
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = (CGRect){10,30,300,44};
[button setTitle:#"Present Action Sheet" forState:UIControlStateNormal];
[button addTarget:self
action:#selector(didTapPresentActionSheetButton:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Another button for demonstration
UIButton *anotherButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
anotherButton.frame = (CGRect){10,90,300,44};
[anotherButton setTitle:#"Another Button" forState:UIControlStateNormal];
[self.view addSubview:anotherButton];
// Custom view with tintColor
TESTCustomView *customView = [[TESTCustomView alloc] initWithFrame:(CGRect){10,150,300,44}];
[self.view addSubview:customView];
}
- (void)didTapPresentActionSheetButton:(id)sender
{
UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:#"Action Sheet"
delegate:nil
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Delete"
otherButtonTitles:#"Other", nil];
[as showInView:self.view];
}
where TESTCustomView is a UIView subclass with implementation as follows:
- (void)drawRect:(CGRect)rect
{
NSLog(#"Drawing with tintColor: %#", self.tintColor);
// Drawing code
[super drawRect:rect];
CGContextRef c = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(c, self.tintColor.CGColor);
CGContextFillRect(c, rect);
}
- (void)tintColorDidChange
{
[self setNeedsDisplay];
}
Running this application in the simulator shows that the custom view's tintColor is automatically animated with the standard UIButton instances in the view controller.
I'm currently creating a drop-down search bar inside my app.
When the drop down animation begins a new -UIView overlays the current one to create the black transparent effect.
However I need to know how I can create a listener that sends an action once my -UIView is touched.
This is how I created the transparent -UIView / -UIWindow
UIWindow* window = [UIApplication sharedApplication].keyWindow;
blackView = [[UIView alloc] initWithFrame: CGRectMake ( 0, 0, 320, 750)];
blackView.backgroundColor = [UIColor blackColor];
[window addSubview:blackView];
blackView.alpha = 0.6;
Image of the drop-down and the -UIView.
Best way is Add UITapGestureRecognizer to your view such like,
UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(TouchViewMethod:)];
gestureRec.numberOfTouchesRequired = 1;
gestureRec.numberOfTapsRequired = 1;
[self.yourViewName addGestureRecognizer:gestureRec];
Method
-(void)TouchViewMethod:(UITapGestureRecognizer *)touch
{
//// Do your stuff;
}
I think you have to implement -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event or you have to add the tapgetsture recognizer to the view.
I am utilizing a split view controller when the user can select an item out of the master view and drag it over to the detail view and place it. While the user is dragging, a UIView appears at the bottom of the detail view where they can drag the item if they change their mind mid-drag and want to remove it. I am having troubles detecting when the touch is in the UIView to remove. Here is what I have so far:
Code that is executed when the user selects the item in the master controller.
MasterViewController:
// Create pointer to window
UIWindow *window = [UIApplication sharedApplication].delegate.window;
// Create draggable view
self.draggableImageView = [[UIImageView alloc] initWithImage:imageView.image];
// Adapt to orientation
[self.draggableImageView rotateToOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
// Hide detail calloutview if there is one
[self.detailViewController hideCalloutView:NO];
// Add drag-view to window
CGPoint location = [gesture locationInView:gesture.view.window];
[self.draggableImageView setCenter:location];
[window addSubview:[self draggableImageView]];
// Animate the marker to jump up
[UIView animateWithDuration:0.10
delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:^{
// Move the marker up
[self.draggableImageView setCenter:CGPointMake(location.x + 75.0f, location.y)];
} completion:^(BOOL finished) {
}];
// Show our remove marker view
[self.detailViewController showRemoveMarkerView];
While the user is dragging, a method in the detail view controller is being called with the updated point and this is the code within that method:
- (void)pointForMarkerDidChange:(UIImageView *)imageView atPoint:(CGPoint)point
{
NSLog(#"Point for Marker Did Change, point = %#", NSStringFromCGPoint(CGPointMake(point.x - 75.0f, point.y)));
UIWindow *window = [UIApplication sharedApplication].delegate.window;
CGRect removeMarkerFrame = [window convertRect:self.removeMarkerView.frame fromView:self.view];
if (CGRectContainsPoint(removeMarkerFrame, point)) {
NSLog(#"YES");
}
}
EDIT: I changed up the following code and it works now.
- (void)pointForMarkerDidChange:(UIImageView *)imageView atPoint:(CGPoint)point
{
UIWindow *window = [UIApplication sharedApplication].delegate.window;
CGRect removeMarkerFrame = [self.removeMarkerView convertRect:self.removeMarkerView.round.frame toView:self.view];
CGPoint point2 = [window convertPoint:point toView:self.view];
if (CGRectContainsPoint(removeMarkerFrame, point2)) {
NSLog(#"YES");
}
}
I have a view controller that alternates 2 views on every touch. Each of the views overrides the drawRect function.
It works when the iPad is in portrait position but for landscape position, the view is shown in the right orientation only once. After that they always appear in portrait orientation.
What's wrong?
In ViewController:
- (void)loadView
{
v= [[View2 alloc] init];
self.view =v;
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
x++;
if (x%2==0)
{
v= [[View2 alloc] init];
}
else {
v=[[View3 alloc] init];
}
self.view = v;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
I fixed the problem. It's simple and makes sense. Basically the problem is that only the FIRST view receives orientation events. When changing the view, event handlers must be hooked up again so that the new view can detect the orientation.
An easy fix is to create a dummy view as the parent and add view1 and view2 as subviews. My new controller code is as follow:
- (void)loadView
{
self.view =[[UIView alloc] init];
v= [[View2 alloc] init];
[self.view addSubview:v];
}
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
x++;
if (v!=NULL)
[v removeFromSuperview];
if (x%2==0)
{
v= [[View2 alloc] init];
}
else {
v=[[View3 alloc] init];
}
[self.view addSubview:v];
}