我有一个包含其他两个UIView的UIView。我试图按下按钮按下它们。这是我在按钮操作中翻转它们的代码:
- (IBAction)btMapView:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView transitionFromView:mView1 toView:m_MapView duration:3 options:UIViewAnimationOptionTransitionFlipFromLeft completion:NULL];
[UIView commitAnimations];
}
- (IBAction)btListView:(id)sender
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
[UIView transitionFromView:m_MapView toView:mView1 duration:3 options:UIViewAnimationOptionTransitionFlipFromRight completion:NULL];
[UIView commitAnimations];
}
我在viewDidLoad中初始化m_MapView,如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
[m_MapView setMapType:MKMapTypeHybrid];
[m_MapView setZoomEnabled:YES];
[m_MapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 42.761738 ;
region.center.longitude = -71.463180;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[m_MapView setRegion:region animated:YES];
m_MapView.showsUserLocation = YES;
[m_MapView setDelegate:self];
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 42.761738;
zoomLocation.longitude = -71.463180;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, .25*METERS_PER_MILE, .25*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [m_MapView regionThatFits:viewRegion];
m_MapView.autoresizingMask =
(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[m_MapView setRegion:adjustedRegion animated:YES];
DisplayMap * myLoc = [[DisplayMap alloc] init];
myLoc.title = @"myLoc";
myLoc.subtitle = @"Main Office";
myLoc.coordinate = region.center;
[m_MapView addAnnotation:myLoc];
}
我的第二个观点就是它上面有一个标签。
当我触摸btMapView按钮时,翻转发生在另一个视图中,但是当我按下btListView按钮时它不会翻转。
任何人都可以解释一下我的错误吗?
感谢您的帮助。
编辑:
通过将我的代码从viewDidLoad移动到viewWillAppear并将两个按钮方法更改为此来实现此功能:
- (IBAction)btMapView:(id)sender
{
[UIView transitionFromView:mView1
toView:m_MapView
duration:1
options:UIViewAnimationOptionTransitionFlipFromLeft | UIViewAnimationOptionShowHideTransitionViews
completion:nil];
}
- (IBAction)btListView:(id)sender
{
[UIView transitionFromView:m_MapView
toView:mView1
duration:1
options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionShowHideTransitionViews
completion:nil];
}
答案 0 :(得分:0)
尝试以下方法:
- (IBAction)btMapView:(id)sender
{
[mView1.superview addSubview: m_MapView];
[UIView transitionFromView:mView1 toView:m_MapView duration:3 options:UIViewAnimationOptionTransitionFlipFromLeft | UIViewAnimationOptionShowHideTransitionViews completion: ^(BOOL finished) { [mView1 removeFromSuperview]; }];
}
- (IBAction)btListView:(id)sender
{
[m_MapView.superview addSubview: mView1];
[UIView transitionFromView:m_MapView toView:mView1 duration:3 options:UIViewAnimationOptionTransitionFlipFromRight | UIViewAnimationOptionShowHideTransitionViews completion:^(BOOL finished) { [m_MapView removeFromSuperview]; }];
}
答案 1 :(得分:0)
在UIButton操作中使用以下代码,这是正确的:
SecondView *second=[[SecondView alloc] init];
[UIView transitionFromView:self.view toView:second.view duration:2.0 options:UIViewAnimationOptionTransitionFlipFromRight completion:nil];
这里SecondView是SecondView的名称。